PHP - MicroMVC A Simple Framework

The MVC pattern is a widely used pattern for developing web applications. PHP has a vast range of open source MVC frameworks that help to ease development. This article presents a lightweight MVC framework that you can use as a bootstrap.

When working on smaller projects, I tend not use any MVC framworks, simply for the reason to maintain a smaller code base. Often I find my self, developing a small MVC framework to make dispatching requests quicker. This article presents a lightweight MVC framework that can be used as a bootstrap. The attached project includes the following directory sturcture.

  • Application
  • System
  • index.php
  • .htaccess

The Application directory contains the controller and view files. The System folder contains the framework classes. index.php is the main entry point and acts as a bootstrap.

Using The Framework

Execution of a request begins with the index.php file, which creates a new instance of System\Application.

  1. require "System/Application.php";  
  2.  
  3. try{  
  4.     $app = new \System\Application(dirname(__FILE__));  
  5.     $app->addRoute("custom route"new \System\Routing\DynamicRoute("/^article/""Article""view"));  
  6.     $app->run();  
  7. }catch(Exception $e){  
  8.     System\Debug::report($e);  
  9. } 

The System\Application class must be instantiated with a single argument that specifies the root path of the application. It attempts to autoload other classes using namespaces. The System\Application class then instantiates a System\Routing\UriRoute, this is the default routing mechanism. The UriRoute, takes the request URI and breaks it down into segments. From these segments, it determines the controller/action names. There are two other route types, System\Routing\StaticRoute and System\Rotuing\DynamicRoute. A StatciRoute, remaps a URI to a controller/action name. This is useful when you want to remap URLs such as domain.com/aboutus.html to a controller/action.

  1. $app = new \System\Application(dirname(__FILE__));  
  2. $app->addRoute("static route"new \System\Routing\StaticRoute("aboutus.html""Home""aboutus"));  
  3. $app->run()

The Application class exposes an addRoute() method, which is used to add custom routes. This method takes two arguments. The first is a route name, which is used to simply refer to the route. The second is a route instance. Notice in the sample code above, that a StaticRoute has been added. This route is placed into a collection and is iterated over when the Application->run() method is executed. As mentioned a DynamicRoute also exists. DynamicRoutes are more flexible and allow you to use regular expressions for mapping. The example below shows how to map any URI that starts with the text "article".

  1. $app = new \System\Application(dirname(__FILE__));  
  2. $app->addRoute("custom route"new \System\Routing\DynamicRoute("/^article/""Article""view"))

As mentioned routes are placed into a collection. The last route to be added, is the first route to be executed. Note; that the default UriRoute is also in the collection and as it is the first route added by the Application class, it is the last route to be executed. This ensures that if your custom routes are not matched, then the UriRoute will be used. All route classes inherit from System\Routing\Route. You can create your own routes by inheriting from this class and implementing the abstract method execute(). The execute() method should return a Boolean value that indicates if the route is valid.

The application life cycle, begins when the run() method is executed. The run() method will iterate over the routes in the routes collection. If a route has been matched the controller/action name is determined and the controller is dispatched. Controllers must inherit from System\Controller. When a controller is instantiated, several methods re called. The first is an init() method, which initializes the controller. This method acts like the class constructor. The next method is the load() method. This method is intended to be overwritten. It provides a way to execute common code for all actions. The next method is the action method. Action methods must return an action result. An action result is either a scalar, array or an object. If you return an array, the framework will encode the array to JSON and send it to the browser. The content type will also be changed to "application/json". If an object is returned, then the object must implement a public render() method, which returns a string. Another action result is a View. Views are not explicitly returned. If you want to return a view, you must create the view in the Views directory, then use the controllers view() method to return the view as shown in the example code below.

  1. public function index(){  
  2.     /** Returns a php template **/  
  3.     return $this->view();  
  4. } 

By Default calling the view() method without a parameter will return a view with the action name. If you want to return a different view, you can specify it as an argument of the view() method as shown below.

  1. public function index(){  
  2.     /** Returns a php template **/  
  3.     return $this->view("another_view");  
  4. } 

Notice, that the file extension is not included.

Data from the controller to the view is passed by a ViewBag. The System\Controller class exposes a protected property "viewBag". This viewBag is just a wrapper around an array. You can set data into the viewBag using the code below.

  1. $this->viewBag->message = "Hello World"
Download

No comments:

Post a Comment