/** * Generate an url by a registred route * * @param string $routeName * @param array $params * @return string */ public function generate($routeName, array $params = array()) { if (!$this->collection->has($routeName)) { throw new RouterException('Route not registred: ' . $routeName); } $routes = $this->collection->getAll(); $route = $routes[$routeName]; $controllerUrl = null; $actionUrl = null; foreach ($route->getController() as $key => $value) { $controllerUrl = $key; } foreach ($route->getAction() as $key => $value) { $actionUrl = $key; } return $controllerUrl . '/' . $actionUrl . '/' . implode('/', $this->detectParams($route, $params)); }
<?php use Attw\Router\Route; use Attw\Router\RoutesCollection; /** * If you change the name of this variable, * go to Application/Application.php and change there too */ $routesCollection = new RoutesCollection(); $routesCollection->add(new Route('index', null, array('controller' => 'Index', 'action' => 'index'))); /** * @example * $routesCollection->add( new Route( 'post_read', 'id', array( * 'controller' => array( * 'post' => 'Posts' * ), * 'action' => 'read' * ) ) ); */
/** * RoutesCollection */ public function __construct(RoutesCollection $collection) { $this->routes = $collection->getAll(); }