├── .htaccess ├── README.md ├── controller └── ProduitController.php ├── model ├── Connection.php └── Produit.php ├── roote.php └── view └── produit ├── create.php ├── edit.php └── index.php /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ roote.php?p=$1 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Welcome to the PHP MVC architecture 2 | 3 | This is a simple MVC design pattern for building web applications in PHP. It's free and open source. 4 | 5 | The live coding explains how the mvc architecture is put together, building it step-by-step, from scratch. If you've attended the the live coding, then you'll already know how to use it. If not, please follow the instructions below. 6 | 7 | ## Starting an application by creating the folders 8 | 9 | 1. models for the m 10 | 2. view for the v 11 | 3. controllers for the c 12 | 4. create route or index file 13 | 5. create .htaccess to redirect the http request to router 14 | ## .htaccess 15 | RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ roote.php?p=$1 16 | This line of code takes the text of the url and redirects it to the route.php file with a GET request. 17 | 18 | ## router 19 | most of the php frameworks (laravel , symfony.. etc) have a convention on http request, that means url is created as yourdomain/controller/action/parameter. 20 | therefore our router allows to split this url to find the controler , the method and the parameter if exists. 21 | 22 | #controller 23 | Controllers respond to user actions (clicking on a link, submitting a form etc.). Controllers are stored in the `Controller` folder. 24 | the file have the same name of controller example :the class HomeController stored in file HomeController.php 25 | every controller have is action : 26 | { 27 | index : display all elements, 28 | create : form for create new element, 29 | save : save element , 30 | edit: form for edit element , 31 | update : update element , 32 | and delete or destroy: delete element 33 | } 34 | Example : 35 | ```php 36 | /** 37 | * doc of controller class 38 | * @ param 39 | * @return void 40 | */ 41 | class ProduitController 42 | { 43 | 44 | public function __construct() 45 | { 46 | 47 | } 48 | 49 | public function index() 50 | { 51 | } 52 | public function create() 53 | { 54 | } 55 | 56 | public function save() 57 | { 58 | } 59 | 60 | public function edit($id) 61 | { 62 | } 63 | 64 | public function update($id) 65 | { 66 | } 67 | public function delete($id) 68 | { 69 | } 70 | 71 | 72 | } 73 | ``` 74 | ## Views 75 | Views are used to display information (normally HTML). View files go in the `View/produit` folder (view for ProduitController). 76 | Views can be in one of two formats: standard PHP, but with just enough PHP to show the data. 77 | No database access or anything like that should occur in a view file. You can render a standard PHP view in a controller, 78 | optionally passing in variables, like this: 79 | ```php 80 | /** 81 | * get all produict from database and send them to view index.php. 82 | * the view have same name of action 83 | * @return void 84 | */ 85 | public function index() 86 | { 87 | $produits=Produit::select(); 88 | require_once __DIR__."/../view/produit/index.php"; 89 | } 90 | ``` 91 | ## Models 92 | 93 | Models are used to get and store data in your application. They know nothing about how this data is to be presented in the views. 94 | Model use [PDO](http://php.net/manual/en/book.pdo.php) to access the database. 95 | They're stored in the `model` folder . 96 | for example general model for create connection and make request SQL : 97 | ```php 98 | class Connection 99 | { 100 | private $servername = "localhost"; 101 | private $username = "root"; 102 | private $password = ""; 103 | private $database="yourdatabasename"; 104 | private $conn; 105 | 106 | public function __construct() 107 | { 108 | 109 | try { 110 | $this->conn = new PDO("mysql:host=$this->servername;dbname=$this->database", $this->username, $this->password); 111 | // set the PDO error mode to exception 112 | 113 | 114 | } catch(PDOException $e) 115 | { 116 | echo "Connection failed: " . $e->getMessage(); 117 | } 118 | } 119 | 120 | public function insert($table,$tableCln,$tableVal) 121 | { 122 | $names=""; 123 | $values=""; 124 | $vrls=""; 125 | for ($i=0; $i 0) 128 | { 129 | $vrls=","; 130 | } 131 | $names.=$vrls."`".$tableCln[$i]."`"; 132 | $values.=$vrls."'".$tableVal[$i]."'"; 133 | } 134 | $str="INSERT INTO `$table`(".$names.") VALUES (".$values.")"; 135 | $query=$this->conn->prepare($str); 136 | $query->execute(); 137 | } 138 | 139 | public function selectAll($table) 140 | { 141 | $query=$this->conn->prepare("SELECT * FROM `$table`"); 142 | $query->execute(); 143 | return $query->fetchAll(PDO::FETCH_ASSOC); 144 | } 145 | public function selectOne($table,$id) 146 | { 147 | $query=$this->conn->prepare("SELECT * FROM `$table` where id=$id"); 148 | $query->execute(); 149 | return $query->fetchAll(PDO::FETCH_ASSOC)[0]; 150 | } 151 | public function update($table,$tableCln,$tableVal,$id) 152 | { 153 | $names=""; 154 | $vrls=""; 155 | for ($i=0; $i 0) 158 | { 159 | $vrls=","; 160 | } 161 | $names.=$vrls."`".$tableCln[$i]."`='".$tableVal[$i]."'"; 162 | } 163 | $str="UPDATE $table SET $names WHERE id=$id"; 164 | $query=$this->conn->prepare($str); 165 | $query->execute(); 166 | } 167 | public function delete($table,$id) 168 | { 169 | $query=$this->conn->prepare("DELETE FROM `$table` WHERE id=$id"); 170 | $query->execute(); 171 | } 172 | 173 | 174 | 175 | } 176 | 177 | ``` 178 | -------------------------------------------------------------------------------- /controller/ProduitController.php: -------------------------------------------------------------------------------- 1 | save(); 32 | header("Location: http://localhost/projetmvc/produit/index"); 33 | } 34 | 35 | public function edit($id) 36 | { 37 | $produit=Produit::edit($id); 38 | require_once __DIR__."/../view/produit/edit.php"; 39 | } 40 | 41 | public function update($id) 42 | { 43 | $name=$_POST['name']; 44 | $quantity=$_POST['quantity']; 45 | $produit=new Produit($name,$quantity); 46 | $produit->update($id); 47 | header("Location: http://localhost/projetmvc/produit/index"); 48 | } 49 | public function delete($id) 50 | { 51 | $produits=Produit::delete($id); 52 | header("Location: http://localhost/projetmvc/produit/index"); 53 | } 54 | 55 | 56 | } -------------------------------------------------------------------------------- /model/Connection.php: -------------------------------------------------------------------------------- 1 | conn = new PDO("mysql:host=$this->servername;dbname=$this->database", $this->username, $this->password); 20 | // set the PDO error mode to exception 21 | 22 | 23 | } catch(PDOException $e) 24 | { 25 | echo "Connection failed: " . $e->getMessage(); 26 | } 27 | } 28 | 29 | public function insert($table,$tableCln,$tableVal) 30 | { 31 | $names=""; 32 | $values=""; 33 | $vrls=""; 34 | for ($i=0; $i 0) 37 | { 38 | $vrls=","; 39 | } 40 | $names.=$vrls."`".$tableCln[$i]."`"; 41 | $values.=$vrls."'".$tableVal[$i]."'"; 42 | } 43 | $str="INSERT INTO `$table`(".$names.") VALUES (".$values.")"; 44 | $query=$this->conn->prepare($str); 45 | $query->execute(); 46 | } 47 | 48 | public function selectAll($table) 49 | { 50 | $query=$this->conn->prepare("SELECT * FROM `$table`"); 51 | $query->execute(); 52 | return $query->fetchAll(PDO::FETCH_ASSOC); 53 | } 54 | public function selectOne($table,$id) 55 | { 56 | $query=$this->conn->prepare("SELECT * FROM `$table` where id=$id"); 57 | $query->execute(); 58 | return $query->fetchAll(PDO::FETCH_ASSOC)[0]; 59 | } 60 | public function update($table,$tableCln,$tableVal,$id) 61 | { 62 | $names=""; 63 | $vrls=""; 64 | for ($i=0; $i 0) 67 | { 68 | $vrls=","; 69 | } 70 | $names.=$vrls."`".$tableCln[$i]."`='".$tableVal[$i]."'"; 71 | } 72 | $str="UPDATE $table SET $names WHERE id=$id"; 73 | $query=$this->conn->prepare($str); 74 | $query->execute(); 75 | } 76 | public function delete($table,$id) 77 | { 78 | $query=$this->conn->prepare("DELETE FROM `$table` WHERE id=$id"); 79 | $query->execute(); 80 | } 81 | 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /model/Produit.php: -------------------------------------------------------------------------------- 1 | name=$name; 16 | $this->quantity=$quantity; 17 | } 18 | 19 | 20 | public function save() 21 | { 22 | $ctn=new Connection(); 23 | $ctn->insert($this->table,["name","quantity"],[$this->name,$this->quantity]); 24 | } 25 | 26 | public static function select() 27 | { 28 | $ctn=new Connection(); 29 | return $ctn->selectAll("produits"); 30 | } 31 | 32 | public static function delete($id) 33 | { 34 | $ctn=new Connection(); 35 | return $ctn->delete("produits",$id); 36 | } 37 | 38 | 39 | public static function edit($id) 40 | { 41 | $ctn=new Connection(); 42 | return $ctn->selectOne("produits",$id); 43 | } 44 | 45 | public function update($id) 46 | { 47 | $ctn=new Connection(); 48 | $ctn->update($this->table,["name","quantity"],[$this->name,$this->quantity],$id); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /roote.php: -------------------------------------------------------------------------------- 1 | $action($params[2]); 20 | 21 | } 22 | else 23 | { 24 | $obj->$action(); 25 | } 26 | }else 27 | { 28 | echo "page not found"; 29 | exit(); 30 | } 31 | }else 32 | { 33 | $action="index"; 34 | $obj->$action(); 35 | } 36 | }else 37 | { 38 | echo "page not found"; 39 | } 40 | } -------------------------------------------------------------------------------- /view/produit/create.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | create produit 7 | 8 | 9 | 10 |
11 | 12 |
13 | 14 |
15 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /view/produit/edit.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | edit produit 7 | 8 | 9 |

Edit Produit

10 |
11 | 12 |
13 | 14 |
15 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /view/produit/index.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | "; 24 | } 25 | ?> 26 | 27 |
idnamequantityaction
".$produit['id']."".$produit['name']."".$produit['quantity']." 21 | delete 22 | edit 23 |
28 | 29 | --------------------------------------------------------------------------------