Last active
August 29, 2015 14:00
-
-
Save harikt/c9fffcb4d22bc79fe83e to your computer and use it in GitHub Desktop.
Aura.View Router Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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
Now trying to create the route object I am getting an exception .