Skip to content

Instantly share code, notes, and snippets.

@harikt
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harikt/c9fffcb4d22bc79fe83e to your computer and use it in GitHub Desktop.
Save harikt/c9fffcb4d22bc79fe83e to your computer and use it in GitHub Desktop.
Aura.View Router Helper
<?php
namespace Aura\Framework_Project\_Config;
use Aura\Di\Config;
use Aura\Di\Container;
class Common extends Config
{
public function define(Container $di)
{
$di->params['Hari\View\Helper\Router']['router'] = $di->lazyGet('web_router');
$di->params['Aura\View\HelperLocator']['registry']['router'] = $di->lazyNew('Hari\View\Helper\Router');
$di->setter['Hari\Controller\Post']['setRenderer'] = $di->lazyNew('Aura\View\TwoStep');
}
public function modify(Container $di)
{
$this->modifyWebRouter($di);
$this->modifyWebDispatcher($di);
}
public function modifyWebRouter(Container $di)
{
$router = $di->get('web_router');
$router->add('hello', '/')
->setValues(array('controller' => 'hello'));
$router->add('contact', '/contact')
->setValues(array('controller' => 'contact'));
$router->add('post.list', '/post')
->setValues(array(
'controller' => 'post',
'action' => 'actionList'
));
$router->add('post.add', '/post/add')
->setValues(array(
'controller' => 'post',
'action' => 'actionAdd'
))
->addServer(array(
'REQUEST_METHOD' => 'POST|GET'
));
$router->add('post.edit', '/post/edit/{id}')
->setValues(array(
'controller' => 'post',
'action' => 'actionEdit'
))
->addTokens(array(
'id' => '\d+'
))
->addServer(array(
'REQUEST_METHOD' => 'POST|GET'
));
$router->add('post.delete', '/post/delete/{id}')
->setValues(array(
'controller' => 'post',
'action' => 'actionDelete'
))
->addTokens(array(
'id' => '\d+'
))
->addServer(array(
'REQUEST_METHOD' => 'POST'
));
}
public function modifyWebDispatcher($di)
{
$dispatcher = $di->get('web_dispatcher');
$dispatcher->setObject('hello', function () use ($di) {
$response = $di->get('web_response');
$response->content->set('Hello World!');
});
$dispatcher->setObject('contact', function () use ($di) {
$response = $di->get('web_response');
$connection_locator = $di->get('connection_locator');
$connection = $connection_locator->getWrite();
$response->content->set('Hello from contact');
});
$dispatcher->setObject('post', $di->lazyNew('Hari\Controller\Post'));
}
}
<?php
/**
*
* This file is part of the Aura project for PHP.
*
* @package Aura.Framework
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
*/
namespace Hari\View\Helper;
use Aura\View\Helper\AbstractHelper;
use Aura\Router\Router as AuraRouter;
/**
*
* Generates route links.
*
* @package Aura.View
*
*/
class Router extends AbstractHelper
{
/**
*
* A router object.
*
* @var Router
*
*/
protected $router;
/**
*
* Constructor.
*
* @param Router $router The router object.
*
*/
public function __construct(AuraRouter $router)
{
$this->router = $router;
}
/**
*
* Returns a route by name; optional interpolates data into it.
*
* @param string $name The route name to look up.
*
* @param array $data The data to interpolate into the URI; data keys
* map to param tokens in the path.
*
* @return string|false A URI path string if the route name is found, or
* boolean false if not.
*
*/
public function __invoke($name, array $data = [])
{
return $this->router->generate($name, $data);
}
}
@harikt
Copy link
Author

harikt commented May 2, 2014

Oh lol. I am not having a post.view route ........... :P .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment