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';
}
?>