Implementing Routes in PHP for MVC Frameworks Tutorial

In this tutorial, we will explore the creation of a straightforward PHP Routing System for a PHP project that utilizes an MVC (Model, View, and Controller) framework. Throughout this tutorial, I will help you grasp the concept of routing within a PHP project and provide PHP Scripts to create a basic web application with routing functionality.

But before we delve into the coding part, let's first understand the purpose of routing within the context of the MVC architectural pattern.

What is Routing in PHP MVC Framework?

In essence, Routing is a process within the script that maps the intended Controller and Action to be executed based on the requested URL. It offers the advantage of cleaner URLs and enhances the overall structure of your web application.

Implementing a Routing System in your PHP project can also result in the generation of SEO-friendly URLs. For example, consider a simple eCommerce System in PHP that includes a list.php file displaying a list of products. This file is typically associated with the Product Controller of the system's framework. Instead of using a URL like [https://sampleStore.com/index.php?controller=Product&view=list], you can have a cleaner and more user-friendly URL like [https://sampleStore.com/product/list]

Now, let's move on to the coding part of this tutorial.

As mentioned earlier, I will provide a simple web application developed in the PHP programming language that incorporates a Routing System to offer you a practical demonstration of the process.

The application that we'll create contains the following files:

- controllers
-- Index.php
-- Users.php
- .htaccess
- index.php
- routes.php

Modifying the URL Rule using .htaccess

First, we'll start by creating a .htaccess file that includes a script to modify or rewrite the URL rules for our application.


#Activate ModRewrite
RewriteEngine On

#let the folders and files to be access
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#Rewrite URL
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

On the script provided above, it shows that the page will be redirected directly to index.php except of the URL provided leads to a folder or files. The parameters on the URL will serve as the route.

Creating a Routing Class?

Next, let's create a PHP class for the routing system. In this example, the file is known as routes.php. The file contains the following script:

  1. <?php
  2. /**
  3.   * List of routes
  4.   * Data Type = Array
  5.   */
  6.  
  7. $routes = [
  8. [
  9. "pattern" => '/^index$/',
  10. "method" => "ANY",
  11. "controller" => "Index"
  12. ],
  13. [
  14. "pattern" => "/^user\/(\d+)$/",
  15. "method" => "GET",
  16. "controller" => "Users",
  17. "page" => "edit"
  18. ],
  19. ];
  20.  
  21. /**
  22.   * Routing Class
  23.   */
  24. class Routing{
  25. // route URI
  26. public $route;
  27. // Controller
  28. public $controller;
  29. // the object to execute from the controller
  30. public $page;
  31. // Post, Get, and other Request Data
  32. public $params =[];
  33. // URI parameter = the controller's object argument(s)
  34. public $args =[];
  35. public function __construct($route =''){
  36. if(!empty($route)){
  37. $this->route = $route;
  38. // Validate route
  39. if($this->validate_route()){
  40. // If route is valid
  41. // load the controller class
  42. include_once("controllers/{$this->controller}.php");
  43.  
  44. if(!$this->page){
  45. // If page is not set
  46. $this->page = 'index';
  47. }
  48. // Initializing the controller with the request data
  49. $controller = new ($this->controller)($this->params);
  50. // Execute the object of the class
  51. $controller->{$this->page}(...$this->args);
  52. echo ob_get_clean();
  53. }else{
  54. // Throw an Error if route is not valid
  55. throw new ErrorException("Undefined Route.");
  56. }
  57. }else{
  58. // Throw an Error if route is not defined
  59. throw new ErrorException("Undefined Route.");
  60. }
  61. }
  62.  
  63. protected function validate_route(){
  64. global $routes;
  65.  
  66. foreach($routes as $route){
  67. /**
  68.   * Check if the route is allowed
  69.   */
  70. if(preg_match($route['pattern'], $this->route, $matches)){
  71. /**
  72.   * Checking if the provided method and the request method are match
  73.   */
  74. if($route['method'] != "ANY"){
  75. if($route['method'] != $_SERVER['REQUEST_METHOD']){
  76. throw new ErrorException("Request Method is denied!");
  77. }
  78. }
  79.  
  80. /**
  81.   * Defining the Arguments
  82.   */
  83. if(isset($matches[1])){
  84. if(!is_array($matches[1])){
  85. $this->args = [$matches[1]];
  86. }else{
  87. $this->args = $matches[1];
  88. }
  89. }
  90. /**
  91.   * Defining the class Object
  92.   */
  93. if(isset($route['page']))
  94. $this->page = $route['page'];
  95. /**
  96.   * Defining the class name
  97.   */
  98. $this->controller = $route['controller'];
  99.  
  100. // Store POST Request Data
  101. $this->params['post'] = $_POST ?? [];
  102. // Store GET Request Data
  103. $this->params['get'] = $_GET ?? [];
  104. // merging matches with params
  105. $this->params = array_merge($this->params, array_filter($matches));
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. }
  112. ?>

The provided script file explains that the list of permitted routes in this application is defined as $routes. Each item in this array contains an array of data with the following details:

  • pattern: The URL pattern to match with the current page URL.
  • method: This will be used to check the request method.
  • controller: The controller name to execute.
  • page: The action from the controller to execute. Default: 'index'

The Routing class includes an object or function that validates the current page URL. If the current URL is found in the list of allowed routes, the controller and other essential information needed for the page will be stored for executing the route or page's intended purpose.

Creating the index file

Now, let's craft the application's index file. This vital file is responsible for loading the routes.php file, which, in turn, initiates the routing class.

  1. <?php
  2. // Load Routing Class
  3. require_once('routes.php');
  4.  
  5. /**
  6.   * Execute Page Routing
  7.   * $_GET['route'] = Route to output
  8.   */
  9. new Routing(rtrim($_GET['route'] ?? '','/'));

Creating the Controllers

Finally, it's time to generate the (2) two controllers required for this application. These controllers house the actions responsible for producing sample content.

Index.php

  1. <?php
  2.  
  3. class Index{
  4. protected $params= [];
  5. function __construct($params = [])
  6. {
  7. // Define provided parmaters
  8. $this->params = $params;
  9. }
  10. public function index(){
  11. // Sample page content
  12. echo "<h1>Sample Index Page</h1>";
  13.  
  14. // Output the prameters available
  15. echo "<pre>";
  16. print_r($this->params);
  17. echo "</pre>";
  18. }
  19. }

Users.php

  1. <?php
  2.  
  3. class Users{
  4. protected $params= [];
  5. function __construct($params = [])
  6. {
  7. // Define provided parmaters
  8. $this->params = $params;
  9. }
  10. public function index(){
  11. // Sample page content
  12. echo "<h1>User List Page!</h1>";
  13. echo "<pre>";
  14. print_r($this->params);
  15. echo "</pre>";
  16. }
  17. public function edit($user_id=''){
  18. // Sample page content with the argument
  19. echo "<h1>EDIT USER with an ID of: [{$user_id}]</h1>";
  20. }
  21. }

Great job! It's time to verify whether the routing system functions as intended.

To conduct a thorough examination of the application's functionality, please navigate to the following URLs:

Note: Replace [appName] with the name assigned to the folder containing your source code files on your system.

http://localhost/[appName]/index

Implementing Routes in PHP for MVC Tutorial

http://localhost/[appName]/user/23

Implementing Routes in PHP for MVC Tutorial

If you'd like to test request method mismatches, you can easily execute a curl command in your terminal or command prompt.

curl -X POST "http://localhost/[appName]/user/23"

Implementing Routes in PHP for MVC Tutorial

DEMO VIDEO:

And there you have it! I hope that this PHP MVC Routing Implementation Tutorial will aid your comprehension of the routing system. Please don't hesitate to download the entire source code I've provided and make improvements as needed. You'll find the download button located beneath this article.

For additional Free Source Code, Tutorials, and articles, delve deeper into this website.

Happy Coding =)

Add new comment