Using Controllers with Aura.Router

1.2 2016-08-24 11:08 UTC


Latest Version on Packagist Software License Build Status Scrutinizer Coverage Quality Score SensioLabsInsight Total Downloads

This library was written to enable users of Aura.Router make use of symfony/laravel "type" controllers.

Already using CFAR , Migrate to: 1.2


You are recommended to make use of the latest available PHP version, but CFAR should run on >=5.5.

CFAR has a dependency, which is Aura.Router. >=1.0 releases require Aura.Router 3.x while <=0.2.1 requires Aura.Router 2.x .

Install CFAR via one of the following methods :

    composer require "adelowo/cfar" : "~1.0"

If you are still using Aura.Router 2.x, please install 0.x

    composer require "adelowo/cfar" : "~0.2"
  • Repo Cloning :
    git clone

If downloading the library without composer or cloning directly from the repository, you'd have to write an autoloader yourself


Cfar doesn't require any special config - other than specifying the class that acts as your controller (namespaced) and method to invoke - you'd still write your routes as specified in Aura.Router's (3.x) doc.

Aura.Router 3 is a big improvement to the much loved router with it being broken into many parts such as a Mapper, Matcher, Route (that contains the matched route).

Internally, Cfar uses PHP's Reflection Api.

By default, Cfar would search and invoke a method called indexAction

Below is a little snippet that shows Aura.Router and Cfar fully integrated, an index.php file and controllers for the routes would be written.


//filename : index.php

use Aura\Router\RouterContainer;

require_once "vendor/autoload.php";

$routeContainer = new RouterContainer();

$routeMapper = $routeContainer->getMap();

$routeMapper->get('', '/blog/{ide}')

$routeMapper->get(null, "/")

$routeMapper->get('dev', '/dev');

    ->handler('\Http\controller\ErrorController'); //`indexAction` would be the invoked method

$routeMatcher = $routeContainer->getMatcher();

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals(

$matched = $routeMatcher->match($request);

if (!$matched) {
	throw new \Aura\Router\Exception("Route does not exists");

foreach ($matched->attributes as $key => $val) {
	$request = $request->withAttribute($key, $val);

 * This is totally optional. But you could use some "Control Inverting", than have `new` wrap all lines of your code
*`SomeContainer` implement `Interop\Container\ContainerInterface`.;
* A neat way to do this is to extend your choosen container and have the `get` method exposed by the interface retrieve the service from the container.
* @see
$container = new SomeContainer(); 

//Add an ORM, Doctrine in this case.
$container['db'] = function ($container) {
    $paths = array("/src/Entities");
    $isDevMode = false;

    $dbParams = [
        'driver' => 'pdo_mysql',
        'user' => 'root',
        'password' => 'xx-xxx-xx-xx',
        'dbname' => 'foo',
    $config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
    return \Doctrine\ORM\EntityManager::create($dbParams, $config);    

//You def' need a logger
$container['logger'] = function ($container) {

    $logger = new \Monolog\Logger("Your App Name");
    $handler = new \Monolog\Handler\SyslogHandler('Owambe');
    $handler->setFormatter(new \Monolog\Formatter\LineFormatter());

    return $logger;

//register X,Y,Z services 

try {

    $cfar = new \Adelowo\Cfar\Cfar($matched , $container);


} catch (\Adelowo\Cfar\CfarException $exception) {
    echo $exception->getMessage(); 

The constructor of the controller would always receive a container. Might be null or a valid one. Your call.

Parameters would be passed to the invoked method in the same order defined in the route.. A method for /users/{id}/{name} should have two parameters, where the first one would be passed the value captured by the router for {id} and vice-versa


namespace Adelowo\Controller;

class BlogController

    protected $container;
    public function __construct(ContainerInterface $container = null)
        $this->container = $container;
    public function showUser($id , $param)
        $db = $this->container->get('db');
        $data = $db->find("User" , $id);

    public function showPdf($name)

            echo $name;

    public function indexAction($id ,$name)
        echo $id. PHP_EOL;
        echo $name;