Building Pure PHP MVC Framework

Yusuf Akhsan H.
4 min readMar 5, 2018

In this post i want to preview about my open source project that i do in Github. It PHP MVC Framework you can https://github.com/yussan/pure-php-mvc, the main reason i am scooping the project is, i want to create a PHP micro framework that has very small size / lightweight, no footprint and very easy to learn for beginners. In addition, all existing modules are pure of the default PHP function is not the dependency of the composer.

Structure and Flow

Of the entire directory can be seen in the picture above, developer just needs to focus on Controllers, Models, Views directories.

Controllers

Everything on this system starts from the controller.

Let me explain with the case when there are web visitors open a page https://web.com/home. The system will check whether there is a controller named HomeController. It is also the same if there is a user visit /profile, The system will check whether there is a controller named ProfileController. Of the pattern then, the first path will be the name of the controller in charge of doing the next process. by default public function index() which be used if the user only accesses 1 level path.

For the current version, this framework only supports up to 2 paths, for example : /directory/detail. System will check DirectoryController and public function detail()

Models and Views

If you see the controller example above, inside the controller there is a model and view being called. Of course each has a different task and explanation.

Models

That job is to process the data, such as create, read, update and, delete. Developers can use any database, or any module required, because here we do not provide any confusion limits for the use of certain modules.

Simply put, this is like using the require function of the models directory and then the results are stored in a variable in the controller file.

Views

All of views is stored in /Views directory. Just create some .php file and fill in with the html or php syntax you normally create when trying hello world.

But how to send data from controller to view ?
If you scroll back up in controller example, it will find the following syntax.

$data = [ 'list' => $list ]; return View::display('DirectoryList', $data);

The Controller uses the view located in the Views/DirectoryList.php, by bringing variable $data in array format.

But in view, we do not have to call the view with

$data['list']

just

$list

Because all the main property in $data will be changed to its own variable.

It’s like magic, how can it happen ?
The answers to this, you can get in the View module available at
/Modules/View.php, technic what we use is extract variable and rendering view using output buffering. Whatever you need about these two things can be found in the official documentation on php.net

The Core

Is the main file called by the server, and early steps before entering the controller. Is located in /config/Bootstrap.php. Core is responsible for validating whether the accessed path is valid, then splitting it according to the above controller rule, if available it will be handled by the authorized controller otherwise it will return 404.

Next Milestone

Routes

This is where the page is accessed by web visitors, for example : https://web.com/directory/home/id, means that route being accessed is /directory/home/id. All routes saved as array and look like this preview.

routes.php

The system will match whether the destination route is available or not, otherwise it will display 404 like web standards. If it is route found / valid. The system will forward the next stage, controllers.

Middleware

The main task is the delimiter between first process and next process , in the case if this framework, i want to put middleware as a delimiter between routes and controllers. Could be used for request validation, authentication, or anything else. Middleware can be made more than one layer as needed.

Closing

thank you for your attention, and if interested, have questions about this project, let’s comment below.

Posted on Utopian.io — Rewarding Open Source Contributors

Originally published at steemit.com on March 5, 2018.

--

--

Yusuf Akhsan H.

if you do not want to do, don’t do. If you want to do, do it effectively and efficiently.