In this tutorial, I will teach you how to Create a Simple MVC Framework in PHP. The tutorial aims to provide you guys with a reference or guide to widen your knowledge and enhance your PHP programming capabilities by understanding and creating a simple MVC Framework. This is useful for you especially if you are planning to create wide broad scope of web applications. This will help you to learn to create your own MVC Framework for your own development and projects.
MVC stands for Model-View-Controller (MVC). This framework is an architecture composed of three (3) logical components which are the Model, View, and Controller. Each architectural element is designed to manage particular application development aspects. It separates the presentation layer from the business logic layer. Initially, it was applied to desktop graphical user interfaces (GUIs). The most widely used industry standard web development framework for building scalable and flexible projects today is MVC. Mobile application design is another usage for it.
Now, before we start building our simple MVC Framework, make sure that the following requirements are already installed on your local machine.
Next, create a new directory for your source codes for this MVC Framework. For XAMPP, place this folder inside the XAMPP's htdocs directory. If you are using WAMP, place it www directory.
Let us create our .htaccess file first. Here, we will configure our app routing and rewrite some conditions and rules. Save this file as .htaccess.
<IfModule mod_rewrite.c> Header set Access-Control-Allow-Origin "*" RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule ^([0-9a-zA-Z].+)$ index.php?main_route=$1 [NC,QSA,L] </IfModule>
The following snippets are the file scripts that hold our main classes for the framework. These PHP Files will be loaded on the index page of the application. Create a new directory inside your source code folder named inc and store the following files scripts in the created directory.
This file contains the PHP Class Script that manages our framework page routing and parameters.
<?php class Routes { public function __construct($parameters){ require_once "controllers/{$controller}.php"; $controller_class = new $controller(); $controller_class->$instance($args); }else{ } }else{ } } }
This PHP Class contains the main methods and objects for our Controllers.
<?php class Controller { protected $render; public function __construct(){ $this->render = new Render(); } public function load_model($model){ $model_main = new Model(); return $model_main->load_model($model); } }
This PHP Class handles our views or the pages' template files.
<?php class Render { public function view($view_filename, $data = []){ $data = (object) $data; include_once("views/{$view_filename}.php"); } }
This PHP Class handles the scripts for loading our Models.
<?php class Model { public function load_model($model){ require_once "models/{$model}.php"; return new $model(); }else{ } } } }
The following snippet is the PHP Script that handles all the classes, methods, and routing of this MVC Framework. Save this file as index.php in your source code folder root directory.
<?php include_once('inc/routes.php'); include_once('inc/controller.php'); include_once('inc/render.php'); include_once('inc/model.php'); $route = $_GET['main_route'] ?? ""; new Routes($route);
Here are the following snippets for creating our Default and Sample Controller for this MVC Framework. Create a new directory in your source code folder naming controllers and store the following snippets on this directory.
The script below is the default controller of this MVC Framework. This controller will run as default if no controller is stated at the URL or route.
<?php class DefaultController extends Controller { public function __construct(){ parent::__construct(); } public function __destruct(){ } public function index(){ $this->render->view('default'); } }
The following controller contains our sample page control for this framework. This controller contains 2 instances which are the index (default) and the load_data method. Here, on the index method, you will see that the controller is passing the parameters or arguments to the PHP file to view. The load_data method is an example instance of the controller that connects with the Model to query data.
<?php class Home extends Controller{ public function index($data=[]){ echo $this->render->view('home', $data); } public function load_data(){ $model = $this->load_model('SampleModel'); $data = $model->sample_query(); $this->render->view("load_data", ["query_data" => $data]); } }
The following snippet is the PHP Script for the example model for this framework. The model contains a method that queries simple data and returns the data to the controller.
<?php class SampleModel { public function sample_query(){ $data = (object) [ [ "id" => 1, "name" => "John Smith", "postion" => "Web Developer" ], [ "id" => 2, "name" => "Samantha Lou", "postion" => "Project Manager" ], [ "id" => 1, "name" => "Mark Cooper", "postion" => "Senior Programmer" ] ]; return $data; } } ?>
Here are the following snippets for the interfaces of our simple application using this simple MVC Framework that we created. Same the files using the given file name otherwise you will need to change the rendered view filename at the controller.
There you go! You can now test the sample application using our created simple PHP MVC Framework. Browse localhost/php_mvc on your preferred browser to test the application if it works properly. I have also provided a complete source code zip file I created for this tutorial. You can download it by clicking the Download Button below this article.
That's the end of this tutorial. I hope this PHP MVC Framework Creation Tutorial will help you with what you are looking for and adds up to your knowledge for developing a web application using the PHP Language.
Explore more on this website for more Tutorials and Free Source Codes.