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

Now trying to create the route object I am getting an exception .

<article>
  <h3><a href="Exception 'Aura\Router\Exception\RouteNotFound' thrown for GET /post

Params: array (
  'controller' => 'post',
  'action' => 'actionList',
)

exception 'Aura\Router\Exception\RouteNotFound' with message 'post.view' in /var/www/heroku/auraphp/vendor/aura/router/src/Router.php:142
Stack trace:
#0 /var/www/heroku/auraphp/src/Hari/View/Helper/Router.php(61): Aura\Router\Router->generate('post.view', Array)
#1 [internal function]: Hari\View\Helper\Router->__invoke('post.view', Array)
#2 /var/www/heroku/auraphp/vendor/aura/view/src/Aura/View/AbstractTemplate.php(165): call_user_func_array(Object(Hari\View\Helper\Router), Array)
#3 /var/www/heroku/auraphp/src/Hari/View/views/post/list.php(3): Aura\View\AbstractTemplate->__call('router', Array)
#4 /var/www/heroku/auraphp/src/Hari/View/views/post/list.php(3): Aura\View\Template->router('post.view', Array)
#5 /var/www/heroku/auraphp/vendor/aura/view/src/Aura/View/Template.php(35): require('/var/www/heroku...')
#6 /var/www/heroku/auraphp/vendor/aura/view/src/Aura/View/TwoStep.php(495): Aura\View\Template->fetch('list')
#7 /var/www/heroku/auraphp/vendor/aura/view/src/Aura/View/TwoStep.php(454): Aura\View\TwoStep->renderView('list', Array)
#8 /var/www/heroku/auraphp/src/Hari/Controller/Post.php(75): Aura\View\TwoStep->render()
#9 /var/www/heroku/auraphp/src/Hari/Controller/Post.php(40): Hari\Controller\Post->render()
#10 [internal function]: Hari\Controller\Post->actionList()
#11 /var/www/heroku/auraphp/vendor/aura/dispatcher/src/InvokeMethodTrait.php(96): ReflectionMethod->invokeArgs(Object(Hari\Controller\Post), Array)
#12 /var/www/heroku/auraphp/vendor/aura/dispatcher/src/Dispatcher.php(118): Aura\Dispatcher\Dispatcher->invokeMethod(Object(Hari\Controller\Post), 'actionList', Array)
#13 /var/www/heroku/auraphp/vendor/aura/dispatcher/src/Dispatcher.php(131): Aura\Dispatcher\Dispatcher->dispatch(Object(Hari\Controller\Post), Array)
#14 /var/www/heroku/auraphp/vendor/aura/dispatcher/src/Dispatcher.php(97): Aura\Dispatcher\Dispatcher->dispatch(Object(Aura\Di\LazyNew), Array)
#15 /var/www/heroku/auraphp/vendor/aura/web-kernel/src/WebKernelDispatcher.php(88): Aura\Dispatcher\Dispatcher->__invoke(Array)
#16 /var/www/heroku/auraphp/vendor/aura/web-kernel/src/WebKernel.php(94): Aura\Web_Kernel\WebKernelDispatcher->__invoke()
#17 /var/www/heroku/auraphp/web/index.php(18): Aura\Web_Kernel\WebKernel->__invoke()
#18 {main}

@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