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][mvc-style-wikipedia], much as other, much larger frameworks like Codeigniter, Symfony or Zend Framework allow you to do.
Pre-requisites
Please see section Routing->Advanced Routing
Instantiating and configuring the Routing Manager
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.
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.