Advanced Routing by using the Routing Manager

Basic Concept

The Routing Manager, which is not enabled by default, is an extension to Yee which enables you to build your applications in MVC style, much as other, much larger frameworks like Codeigniter, Symfony or Zend Framework allow you to do.

Pre-requisites

The Routing Manager uses a cache directory which must be located under your app's root path, and comes already set up within your Yee source archive. The folder 'routing' within the holding cache folder will need read/write permissions on *nix systems for the web user. By using any FTP client set the permissions for this folder to i.e. 777. In the case you are accessing your hosting space by SSH you may execute the following command:

chmod 777 /cache/routing -R 

You may configure any folder to hold your App's Controllers and Models, but for the sake of convenience Yee by default has the App Folder and Folders for Controllers and Models pre-created for you.

Instantiating and configuring the Routing Manager

You must also tell Yee that your are be using the Routing Manager for your Routes to work properly.

The simplest form of doing so is:

  <?php

   require 'Yee/Yee.php';
   \Yee\Yee::registerAutoloader();
   $app = new \Yee\Yee( );
   new \Yee\Managers\RoutingCacheManager( 
                                         array( 
                                              'cache' => __DIR__ . '/cache/routing', 
                                              'controller' => array( __DIR__ . '/App/Controllers' ) 
                                              )
                                        );
   $app->execute();

Whereas you will pass the Routing Manager configuration parameters in the form of an associative array. Allowed parameters are 'cache' - the location of the cache folder and 'controller' - the location where your controllers will be located.

In some occasions you might need more than one location for your controllers, i.e. you requirements is also using a set of subcontrollers. The controller parameter above could also look as follows:

new RoutingCacheManager(
   array(
      'cache'      => __DIR__ . '/cache/routing',
      'controller' => array(
         __DIR__ . '/App/Controllers',
         __DIR__ . '/App/Subcontrollers',
        )
    )
);

Creating your first Controller

Create a file named 'TestController.php' under /App/Controllers. Now add the following code to TestController.php

<?php

use Yee\Managers\Controller\Controller;

class TestController extends Controller
{
    /**
     * @Route('/') 
     * @Name('home.index')
     */
    public function indexAction( )
    {
        /** @var Yee\Yee $yee */
        $app = $this->getYee();
        $app->response()->body( 'This is the home route ' . $this->getName() );
    }
}

Now run the script by invoking 'http://your-host.com/'.

As you can see the annotations are very clear and reflect clearly the intention of this route endpoint. What happens below the surface is rather simple. the @Route('/') tells Yee that the following function indexAction() is to be called for the homepage route. The @Name('home.index') is just a dummy parameter and just used for translating into human readable form. So you should give proper names to your routes. As you can see you could open the above sample with your Web Browser, which basically means we have defined a GET route. If you wanted to create any other HTTP Request Method you can specify this by adding the @Method('POST') below the @Route statement.

Heads Up! You may define as many Routes within a single class as you require.

As you would define a regular Route

<?php
$app = new \Yee\Yee();
$app->get('/books/:id', function ( $id ) {
    //Show book identified by $id
});

you may inject custom values from within the Routing Managers Routing definition.

<?php
use Yee\Managers\Controller\Controller;

class TestController extends Controller
{
    /**
     * @Route('/:variable') 
     * @Name('data.index')
     */
    public function indexAction( $variable )
    {
        /** @var Yee\Yee $yee */
        $app = $this->getYee();
        echo 'The Variable is: '.$variable
    }
}

Regular Expressions as explained in Routing->Parameters will also work when using the Routing Manager.