User Rating: 1 / 5

Star ActiveStar InactiveStar InactiveStar InactiveStar Inactive
 

The router class is responsible for loading up the correct controller. It does nothing else. The value of the controller comes from the URL.

The url will look like this:

http://www.example.com/index.php?rt=news

or if you have htaccess amd mod_rewrite working like this:

http://www.example.com/news
 

As you can see, the route is the rt variable with the value of news. To begin the router class a few things need to be set.

Add this code to the router.class.php file in the application directory

<?php
class router {
private $registry;
private $path;
private $args = array();
public $file;
public $controller;
public $action;
function __construct($registry) {
   $this>registry = $registry;
}

So it does not look like much yet but it is enough to get us started. We can load the router into the registry also.

Add this code to the index.php file

$registry->router = new router($registry);

Now that the router class can be loaded, we can continue with the router class by adding a method to set the controller

directory path.

Add this block of code to the router.class.php file

<?php
function setPath($path) {
  if (is_dir($path) == false)
{
     throw new Exception ('Invalid controller path: `' . $path . '`');
}
  This->path = $path;
}

And to set the controller path in the registry is a simple matter of adding this line to the index.php file

$router->setPath (__SITE_PATH . 'controller');

With the controller path set we can load the controller. We will create a method to called loader() to get the controller and load it. This method will call a getController() method that will decide which controller to load. If a controller is not found then it will default back to the index.

The loader method looks like this.

<?php
public function loader()
{
  $this->getController();
{
if (is_readable($this->file) == false)
{
  echo $this->file;
  die ('404 Not Found');
}
include $file->file;
$class = $this->controller . 'Controller_';
$controller = new $class($this->registry);
if (is_callable(array($controller, $this->action)) == false)
{
$action = 'index';
}
  else
{
$action = $this->action;
}
$controller->$action();
}

The getController method that the loader() method calls does the work. By taking the route variables from the url via $_GET['rt'] it is able to check if a contoller was loaded, and if not default to index. It also checks if an action was loaded. An action is a method within the specified controller. If no action has been declared, it defaults to index. Add the getController method to the router.class.php file.

<?php
private function getController() {
$route = (empty($_GET['rt'])) ? " : $_GET['rt'];
if (empty($route))
{
$route = 'index';
}
else
{
$parts = explode('/', $route);
$this->controller = $parts[0];
if(isset( $parts[1]))
{
$this->action = $parts[1];
}
}
if (emptry($this->controller
{
$this->controller = 'index';
}
if (emptry($this->action))
{
$this->action = 'index';
}
$this->file = $this->path .'/'. $this->controller . '.php';
}
?>

 

Recent Topics
Subject
Post Reply
Open
Recent
The program calculates the PMT
By prmindphp Wed 20 May 2020 6:45 pm Board English Language
1
29
Thu 21 May 2020 5:57 pm By prmindphp
Helper for calculating NPER values
By prmindphp Tue 19 May 2020 6:55 pm Board English Language
1
27
Thu 21 May 2020 5:59 pm By prmindphp
Program that will be used for home loan calculations
By prmindphp Mon 18 May 2020 6:49 pm Board English Language
1
33
Thu 21 May 2020 6:02 pm By prmindphp
Tools that will help calculate car tax
By prmindphp Thu 14 May 2020 7:00 pm Board English Language
1
29
Fri 15 May 2020 12:03 am By prmindphp
Helper to create slidershow, Module Product Slide of MooZiiCart
By prmindphp Thu 30 Apr 2020 6:32 pm Board English Language
0
42
Thu 30 Apr 2020 6:32 pm By prmindphp
Helper in searching products by category and keywords with Module Ajax Search of MooZiiCart
By prmindphp Tue 28 Apr 2020 6:17 pm Board English Language
0
39
Tue 28 Apr 2020 6:17 pm By prmindphp
Helper to restore the system to clear data, Plugin System MRestore
By prmindphp Fri 24 Apr 2020 7:05 pm Board English Language
0
59
Fri 24 Apr 2020 7:05 pm By prmindphp
Creating social share buttons, share the website's content to Social Media With Plugin Content Msocial
By prmindphp Wed 22 Apr 2020 7:05 pm Board English Language
0
65
Wed 22 Apr 2020 7:05 pm By prmindphp
Create Content Marketing and Product with Content Product Match of MooZiiCart
By prmindphp Tue 21 Apr 2020 7:19 pm Board English Language
0
74
Tue 21 Apr 2020 7:19 pm By prmindphp
Filtering for find the product with Module Filter product of MooZiiCart
By prmindphp Thu 09 Apr 2020 6:51 pm Board English Language
0
71
Thu 09 Apr 2020 6:51 pm By prmindphp
Template Megadeal of MooZiiCart for creating websites to sell products online
By prmindphp Fri 03 Apr 2020 6:45 pm Board English Language
0
255
Fri 03 Apr 2020 6:45 pm By prmindphp
Displaying products that customers are interested by Wishlist Module
By prmindphp Fri 27 Mar 2020 5:46 pm Board English Language
0
469
Fri 27 Mar 2020 5:46 pm By prmindphp
Show the Bestsellers Product by Module Bestseller
By prmindphp Wed 25 Mar 2020 6:04 pm Board English Language
0
285
Wed 25 Mar 2020 6:04 pm By prmindphp
Show Products Category by Module Category
By prmindphp Tue 24 Mar 2020 6:07 pm Board English Language
0
303
Tue 24 Mar 2020 6:07 pm By prmindphp
Plugin System MZC Auto Close to enable the open and close odering system
By prmindphp Wed 19 Feb 2020 6:38 pm Board English Language
0
2968
Wed 19 Feb 2020 6:38 pm By prmindphp
MDPartner Component for store client data in CRM system
By prmindphp Sat 25 Jan 2020 3:04 pm Board English Language
0
752
Sat 25 Jan 2020 3:04 pm By prmindphp
i am looking for help
By Anonymous Tue 14 Jan 2020 5:46 pm Board English Language
1
772
Tue 14 Jan 2020 6:03 pm By noppadonsk
Dot Net Training
By Poonaam Fri 10 Jan 2020 2:20 pm Board English Language
0
955
Fri 10 Jan 2020 2:20 pm By Poonaam
MDRental, Area management assistant for rental business
By prmindphp Wed 08 Jan 2020 6:51 pm Board English Language
0
741
Wed 08 Jan 2020 6:51 pm By prmindphp
Fix Gmail Error 404 By Taking Gmail Help Quickly
By Anonymous Wed 08 Jan 2020 2:56 pm Board English Language
0
747
Wed 08 Jan 2020 2:56 pm By Anonymous