/** * @dataProvider hostnamedRegexRoutesProvider */ public function testHostnameRegexRouteGroup($actualHost, $expectedHost, $controller) { Phalcon\Mvc\Router\Route::reset(); $di = new Phalcon\DI(); $di->set('request', function () { return new Phalcon\Http\Request(); }); $router = new Phalcon\Mvc\Router(false); $router->setDI($di); $router->add('/edit', array('controller' => 'posts3', 'action' => 'edit3')); $group = new Phalcon\Mvc\Router\Group(); $group->setHostname('([a-z]+).phalconphp.com'); $group->add('/edit', array('controller' => 'posts', 'action' => 'edit')); $router->mount($group); $_SERVER['HTTP_HOST'] = $actualHost; $router->handle('/edit'); $this->assertEquals($router->getControllerName(), $controller); $this->assertEquals($router->getMatchedRoute()->getHostname(), $expectedHost); }
public function testHostnameRegexRouteGroup() { $this->specify("Router Groups with regular expressions don't work properly", function ($actualHost, $expectedHost, $controller) { \Phalcon\Mvc\Router\Route::reset(); $di = new \Phalcon\DI(); $di->set("request", function () { return new \Phalcon\Http\Request(); }); $router = new \Phalcon\Mvc\Router(false); $router->setDI($di); $router->add("/edit", ["controller" => "posts3", "action" => "edit3"]); $group = new \Phalcon\Mvc\Router\Group(); $group->setHostname("([a-z]+).phalconphp.com"); $group->add("/edit", ["controller" => "posts", "action" => "edit"]); $router->mount($group); $_SERVER["HTTP_HOST"] = $actualHost; $router->handle("/edit"); expect($router->getControllerName())->equals($controller); expect($router->getMatchedRoute()->getHostname())->equals($expectedHost); }, ["examples" => $this->hostnamedRegexRoutesProvider()]); }
public function testHostnameRegexRouteGroup() { Phalcon\Mvc\Router\Route::reset(); $di = new Phalcon\DI(); $di->set('request', function () { return new Phalcon\Http\Request(); }); $router = new Phalcon\Mvc\Router(false); $router->setDI($di); $router->add('/edit', array('controller' => 'posts3', 'action' => 'edit3')); $group = new Phalcon\Mvc\Router\Group(); $group->setHostname('([a-z]+).phalconphp.com'); $group->add('/edit', array('controller' => 'posts', 'action' => 'edit')); $router->mount($group); $routes = array(array('hostname' => 'localhost', 'controller' => 'posts3'), array('hostname' => 'my.phalconphp.com', 'controller' => 'posts'), array('hostname' => null, 'controller' => 'posts3')); foreach ($routes as $route) { $_SERVER['HTTP_HOST'] = $route['hostname']; $router->handle('/edit'); $this->assertEquals($router->getControllerName(), $route['controller']); } }
<?php /* * Define custom routes. File gets included in the router service definition. */ $router = new Phalcon\Mvc\Router(); $router->add('/restore-password', ['controller' => 'signin', 'action' => 'restorePassword']); $router->add('/new-password', ['controller' => 'profile', 'action' => 'newPassword']); $router->add('/change-password', ['controller' => 'profile', 'action' => 'changePassword']); $router->add('/language/{language}', ['controller' => 'language', 'action' => 'index']); $userManagement = new \Phalcon\Mvc\Router\Group(['controller' => 'usermanagement']); $userManagement->setPrefix('/manage-users'); $userManagement->add('', ['action' => 'index']); $userManagement->add('/email', ['action' => 'sendEmail']); $router->mount($userManagement); return $router;
<?php //Create a group with a common module and controller $blog = new \Phalcon\Mvc\Router\Group(array('module' => 'blog', 'controller' => 'posts')); //Hostname restriction $blog->setHostName('blog.mycompany.com'); //All the routes start with /blog $blog->setPrefix('/blog'); //Default route $blog->add('/', array('action' => 'index')); //Add a route to the group $blog->add('/save', array('action' => 'save')); //Add another route to the group $blog->add('/edit/{id}', array('action' => 'edit')); //Add the group to the router $router->mount($blog);
/** * Set the static router service * * @package las * @version 1.0 * * @return void */ protected function router() { $this->_di->set('router', function () { $router = new \Phalcon\Mvc\Router(false); $router->setDefaults(array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); /* * All defined routes are traversed in reverse order until Phalcon\Mvc\Router * finds the one that matches the given URI and processes it, while ignoring the rest. */ $frontend = new \Phalcon\Mvc\Router\Group(array('module' => 'frontend')); $frontend->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); $frontend->add('/:controller/:int', array('controller' => 1, 'id' => 2)); $frontend->add('/:controller[/]?', array('controller' => 1)); $frontend->add('/{action:(buy|contact)}[/]?', array('controller' => 'static', 'action' => 'action')); $frontend->add('/'); // Mount a group of routes for frontend $router->mount($frontend); /** * Define routes for each module */ foreach (array('admin', 'doc') as $module) { $group = new \Phalcon\Mvc\Router\Group(array('module' => $module)); $group->setPrefix('/' . $module); $group->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); $group->add('/:controller/:int', array('controller' => 1, 'id' => 2)); $group->add('/:controller[/]?', array('controller' => 1)); $group->add('[/]?', array()); // Mount a group of routes for some module $router->mount($group); } $router->notFound(array('controller' => 'index', 'action' => 'notFound')); return $router; }); }
<?php /** * @todo переписать на класс, наследующий extends Phalcon\Mvc\Router\Group */ $adminRouter = new \Phalcon\Mvc\Router\Group(['namespace' => 'Admin\\Controllers', 'module' => 'admin', 'controller' => 'admin']); $adminRouter->setPrefix('/admin'); $adminRouter->add('/shows', ['controller' => 'Shows', 'action' => 'similarShows'])->setName('admin-shows'); return $adminRouter;
<?php /** * @todo переписать на класс, наследующий extends Phalcon\Mvc\Router\Group */ $autoAdminRouter = new \Phalcon\Mvc\Router\Group(['namespace' => 'Admin\\Controllers', 'module' => 'admin', 'controller' => 'admin']); $autoAdminRouter->setPrefix('/admin'); $autoAdminRouter->add('', ['controller' => 'crud', 'action' => 'index'])->setName('admin'); $autoAdminRouter->add('/auto/{entity:[a-zA-Z]+}/{action:(edit|delete)}/:params', ['controller' => 'crud', 'entity' => 1, 'params' => 3])->setName('admin-action'); // такие действия должны быть только методом POST + проверка токенов //$admin->addPost( $autoAdminRouter->addPost('/auto/{entity:[a-zA-Z]+}/{action:(save|delete)}/:params', ['controller' => 'crud', 'entity' => 1, 'params' => 3])->setName('admin-action-post'); $autoAdminRouter->addGet('/auto/{entity}', ['controller' => 'crud', 'entity' => 1, 'action' => 'list'])->setName('admin-entity'); $autoAdminRouter->add('/login', ['action' => 'login'])->setName('admin-login'); $autoAdminRouter->add('/logout', ['action' => 'logout'])->setName('admin-logout'); return $autoAdminRouter;
<?php $router = new Phalcon\Mvc\Router(); //Create a group with a common module and controller $blog = new Phalcon\Mvc\Router\Group(array('module' => 'blog', 'controller' => 'index')); //All the routes start with /blog $blog->setPrefix('/blog'); //Add a route to the group $blog->add('/save', array('action' => 'save')); //Add another route to the group $blog->add('/edit/{id}', array('action' => 'edit')); //This route maps to a controller different than the default $blog->add('/blog', array('controller' => 'about', 'action' => 'index')); //Add the group to the router $router->mount($blog);
<?php $miniAdminRouter = new \Phalcon\Mvc\Router\Group(['namespace' => 'MiniAdmin\\Controllers', 'module' => 'miniadmin', 'controller' => 'index']); $miniAdminRouter->setPrefix('/miniadmin'); $miniAdminRouter->add('', ['controller' => 'index', 'action' => 'index'])->setName('mini-admin-index'); $miniAdminRouter->addGet('/create', ['controller' => 'index', 'action' => 'create'])->setName('mini-admin-create'); $miniAdminRouter->addPost('/create', ['controller' => 'index', 'action' => 'create'])->setName('mini-admin-save'); $miniAdminRouter->addGet('/edit/{id:[\\d]+}', ['controller' => 'index', 'action' => 'edit'])->setName('mini-admin-edit'); $miniAdminRouter->addPost('/edit/{id:[\\d]+}', ['controller' => 'index', 'action' => 'edit'])->setName('mini-admin-edit'); return $miniAdminRouter;
*/ $router = new \Phalcon\Mvc\Router(FALSE); $router->setDefaults(array('module' => 'frontend', 'controller' => 'index', 'action' => 'index')); /* * All defined routes are traversed in reverse order until Phalcon\Mvc\Router * finds the one that matches the given URI and processes it, while ignoring the rest. */ $frontend = new \Phalcon\Mvc\Router\Group(array('module' => 'frontend')); $frontend->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); $frontend->add('/:controller/:int', array('controller' => 1, 'id' => 2)); $frontend->add('/:controller[/]?', array('controller' => 1)); $frontend->add('/{action:(buy|contact|terms)}[/]?', array('controller' => 'static', 'action' => 'action')); $frontend->add('/'); // Mount a group of routes for frontend $router->mount($frontend); /** * Define routes for each module */ //foreach ($this->getModules() as $module => $options) { foreach (array('backend' => array('alias' => \Phalcon\DI::getDefault()->getShared('config')->app->admin_uri), 'documentation' => array('alias' => 'doc')) as $module => $options) { $group = new \Phalcon\Mvc\Router\Group(array('module' => $module)); $group->setPrefix('/' . (isset($options['alias']) ? $options['alias'] : $module)); $group->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); $group->add('/:controller/:int', array('controller' => 1, 'id' => 2)); $group->add('/:controller[/]?', array('controller' => 1)); $group->add('[/]?', array()); // Mount a group of routes for some module $router->mount($group); } $router->notFound(array('controller' => 'index', 'action' => 'notFound')); return $router;