Calcetines Extreme

Calcetines Extreme
Take care of you using the best socks

Monday, October 19, 2020

How to setup PHP mvc minimun model without any framework

 Problem:

You need to create a minimum web site or web app using php model but it's preferred to use any framework to avoid load the system without any unnecessary code.

Solution:

Create the following folder and fields structure in the server.
\
\\controllers\sample_controler.php
\\db\connection.php
\\models\sample_model.php
\\views\sample_view.html
\\index.php

Use the next code in each file:

\\controllers\sample_controler.php
<?php
require_once("models/sample_model.php");
$samp=new sample_model();
$data=$samp->get_samplequery();
require_once("views/sample_view.html");
?>
\\db\connection.php
<?php
class Connect{
    public static function connection(){
        $connection=new mysqli("localhost", "root", "pwd", "mvc");
        $connection->query("SET * FROM table 'UTF8' ");
        return $connection;
    }
}
?>

\\models\sample_model.php
<?php
class sample_model{
    private $DB;
    private $sample;
    public function __construct(){
        $this->DB=Connect::connection();
        $this->sample=array();
    }
    public function get_samplequery(){
        $query=$this->db->query("select * from table;");
         ...
        return $this->query;
    }
}
?>

\\views\sample_view.html
<html lang="es">
    <head>
        <meta charset="UTF8" />
        <title>Sample</title>
    </head>
        <?php echo "put your database query here"        ?>
</html>

\index.php
<?php
require_once("db/connection.php");
require_once("controllers/sample_controler.php");
?>