Posts

Showing posts with the label mvc

Creating my First PHP Framework : Day 4

Image
After knowing which controller to load with App.php  , we need to know what the view corresponding, or simply create a functions to load the view in Controller.php in this file we find a function called view , it has 3 parameters -  $view : the view file name -  $data = [] : array of a view variables ,if we want send variables to the view -  $template = " default " : The view layout it loads by default , default.php exemple : $this -> view( ' pages/home ' , $data = [ ' name ' => $name ]); the second function model is just requiring the model for the controller require_once " ../app/models/ " . $model . " .php " ; and getting instant of it return new $model (); Tip : ob_start   is more like to save the view file content inside  a variable

Creating my First PHP Framework : Day 3

Image
The Controller Ohhh this magic piece of code  ,  to simplify things the role of controller is knowing which part of code to load . The idea here is a creating a class to auto load a model  and a functions to load view(template) , but... wait we have to know witch controller to load first , don't worry the is the Magic App.php file : Each time we load an url we have to decompose with (  URLparser function ) it into BASE_URL/CONTROLLER/ACTION/PARAMS in this file we take the url , extract the controller'name  as array   , after we require the controller file(after checking if exist) require_once " ../app/controllers/ " . $this -> controller . ' .php ' ; Lastly , we use  call_user_func_array to call the URL call_user_func_array ( [ $this -> controller , $this -> method ], $this -> params ); The framework github

Creating my First PHP Framework : Day 2

Image
First things first , i'needed a file (init.php) witch requires all other components of the framework , also all the static variables such as APP and CSS directories so i called it init.php                                                       <?php define ( ' ROOT ' , dirname ( __DIR__ )); define ( ' APP ' , ROOT . "/ app /" ); define ( ' WEBROOT ' , " http:// " . $_SERVER [ ' HTTP_HOST ' ] . dirname ( $_SERVER [ ' PHP_SELF ' ]) . " / " ); define ( ' CSS ' , WEBROOT . " css/ " ); define ( ' IMG ' , WEBROOT . " img/ " ); define ( ' JS ' , WEBROOT . " js/ " ); define ( ' DS ' , ROOT . " / " ); require_once ROOT . ' /vendor/autoload.php ' ; require_once APP . ' /core/App.php ' ; require_once APP . ' /database.php ' ; Also i need to require 3 files autoload.php : composer's a...

Creating my First PHP Framework : Day 1

Image
Why ?  So why should you write your own framework? Firstly, it is a great learning experience. You will get to learn PHP inside-out. You will get to learn object-oriented programming, design patterns and considerations. More importantly, you have complete control over your framework. Your ideas can be built right into your framework. Although always not beneficial, you can write coding conventions/functions/modules the way you like. 11 Months ago i started building MVC Php framework ,   the idea was create a simple flixible framework   which any developer can use it without problem ORM The ORM  is a kind of abstraction of database , it avoids you to to write long SQL statmemnts using  functions eg save to INSERT data or to find   to SELECT data use Cake\ORM\TableRegistry ; $data = [ ' title ' => ' My first article ' , ' body ' => ' It is a great article ' , ' user_id ' => 1 , ...