/** * @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()]); }
<?php use PhalconSeed\Routes\AuthRoute; use PhalconSeed\Routes\UserRoute; use PhalconSeed\Routes\RoleRoute; use PhalconSeed\Routes\PermissionRoute; use PhalconSeed\Routes\LogRoute; /* * Define custom routes. File gets included in the router service definition. */ $router = new Phalcon\Mvc\Router(false); // Add the group of auth route to the router $router->mount(new AuthRoute()); // Add the group of user route to the router $router->mount(new UserRoute()); // Add the group of role route to the router $router->mount(new RoleRoute()); // Add the group of permission route to the router $router->mount(new PermissionRoute()); // Add the group of log route to the router $router->mount(new LogRoute()); return $router;
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;
$loader->register(); //Dependency Injection $di = new \Phalcon\DI\FactoryDefault(); $di->set('db', function () { $db = new \Phalcon\Db\Adapter\Pdo\Mysql(["host" => "localhost", "username" => "phalcon", "password" => "TvevzMjmJmE7EHr4", "dbname" => "learning_phalcon"]); return $db; }); $di->set('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views'); $view->registerEngines([".volt" => "Phalcon\\Mvc\\View\\Engine\\Volt"]); return $view; }); $di->set('router', function () { $router = new \Phalcon\Mvc\Router(); $router->mount(new Routes()); //Call it whatever name your php file is return $router; }); //Session start $di->setShared('session', function () { $session = new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); //Flash data (Temporary data) $di->set('flash', function () { $flash = new \Phalcon\Flash\Session(['error' => 'alert alert-danger', 'success' => 'alert alert-success', 'notice' => 'alert alert-info', 'warning' => 'alert alert-warning']); return $flash; }); //Meta-data
/** * 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; }); }
}); //Load Views $di->set('view',function(){ $view=new \Phalcon\Mvc\View(); $view->setViewsDir('../app/views'); $view->registerEngines([ '.volt'=> 'Phalcon\Mvc\View\Engine\Volt' ]); return $view; }); //Router $di->set('router', function(){ $router =new \Phalcon\Mvc\Router(); $router->mount(new GlobalRoutes()); return $router; }); //Session $di->setShared('session', function(){ $session=new \Phalcon\Session\Adapter\Files(); $session->start(); return $session; }); $di->setShared('component',function(){ $obj=new stdClass(); $obj->helper= new \Component\Helper(); $obj->user=new \Component\User(); return $obj;
//Transform the slug removing the dashes return str_replace('-', '', $slug); }); // 路由分组(Groups of Routes) class BlogRoutes extends Phalcon\Mvc\Router\Group { public function initialize() { $this->setPaths(array('module' => 'blog', 'namespace' => 'Blog\\Controllers')); $this->setPrefix('/blog'); $this->add('/save', array('action' => 'save')); $this->add('/edit/{id}', array('action' => 'edit')); $this->add('/blog', array('controller' => 'blog', 'action' => 'index')); } } $router->mount(new BlogRoutes()); // 路由命名(Naming Routes) $router->add("/posts/{year}/{title}", "Posts::show")->setName("show-posts"); $url->get(array("for" => "show-posts", "year" => "2012", "title" => "phalcon-1-0-released")); // 匹配回调函数(Match Callbacks) class AjaxFilter { public function check($uri, $route) { return $_SERVER['HTTP_X_REQUESTED_WITH'] == 'xmlhttprequest'; } } $router->add('...', array())->beforeMatch(array(new AjaxFilter(), 'check')); // 限制主机名(Hostname Constraints) $router->add('...', array())->setHostName('admin.company.com'); $router->add('...', array())->setHostName('([a-z+]).company.com');
<?php $router = new \Phalcon\Mvc\Router(true); $router->setDefaults(['namespace' => 'Controller', 'controller' => 'index', 'action' => 'index']); $authGroup = new \Phalcon\Mvc\Router\Group(['namespace' => 'Controller', 'controller' => 'auth']); $authGroup->addGet('/signin', ['action' => 'signIn'])->setName('signIn'); $authGroup->addGet('/signup', ['action' => 'signUp'])->setName('signUp'); $authGroup->addPost('/signin', ['action' => 'signInPost'])->setName('signInPost'); $authGroup->addPost('/signup', ['action' => 'signUpPost'])->setName('signUpPost'); $router->mount($authGroup); return $router;
/** * This methods registers the services to be used by the application */ protected function _registerServices() { $di = new \Phalcon\DI\FactoryDefault(); // Registering a Config shared-service $di->setShared('config', function () { $config['main'] = (include ROOT_DIR . "/config/main.php"); $config['db'] = (include ROOT_DIR . "/config/db.php"); $config['router'] = (include ROOT_DIR . "/config/router.php"); return new \Phalcon\Config($config); }); $config = $di->get('config'); // Change FactoryDefault default Router service $di->getService('router')->setDefinition(function () use($config) { $router = new \Phalcon\Mvc\Router(); //Setup routes from /config/router.php foreach ($config->router->toArray() as $key => $value) { $router->add($key, $value); } // Mount each module routers $router->mount(new \App\Frontend\Router()); $router->mount(new \App\Backend\Router()); // URI_SOURCE_SERVER_REQUEST_URI $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); // Remove trailing slashes automatically $router->removeExtraSlashes(true); // Setting a specific default using an array // DefaultModule // DefaultNamespace // DefaultController // DefaultAction $router->setDefaults(array('modul' => 'frontend', 'namespace' => 'App\\Frontend\\Controllers\\', 'controller' => 'index', 'action' => 'index')); // Set 404 paths $router->notFound(["controller" => "error", "action" => "error404"]); return $router; }); // Registering a View shared-service $di->setShared('view', function () { return new \Phalcon\Mvc\View(); }); // Registering a Mysql shared-service $di->setShared('mysql', function () use($config) { return new \Phalcon\Db\Adapter\Pdo\Mysql($config->db->mysql->toArray()); }); // Registering a Db shared-service $di->setShared('db', function () use($di) { return $di->get('mysql'); }); // Registering a Url shared-service $di->setShared('url', function () { $url = new \Phalcon\Mvc\Url(); $url->setBaseUri('/'); return $url; }); // Start the Session service $di->get('session')->start(); // Setup the Crypt service $di->get('crypt')->setMode(MCRYPT_MODE_CFB)->setKey($config->main->crypt_key); // Setup the Security service $di->get('security')->setWorkFactor(12); // Registering a custom authentication shared-service $di->setShared('auth', function () { return new \App\Common\Libs\Auth(); }); $this->setDI($di); }
* Start the session the first time some component request the session service */ $di->set('session', function () { $session = new SessionAdapter(); $session->start(); return $session; }); $di->setShared('dispatcher', function () { $eventsManager = new Phalcon\Events\Manager(); $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) { /* @var $dispatcher Phalcon\Mvc\Dispatcher */ if ($event->getType() == 'beforeException') { $ctrl = $dispatcher->getActiveController(); if ($ctrl instanceof \Test\Controllers\API\ApiControllerBase) { $dispatcher->forward(array('namespace' => '\\', 'controller' => 'error', 'action' => 'api', 'params' => array('message' => $exception->getMessage()))); return false; } } }); $dispatcher = new Phalcon\Mvc\Dispatcher(); $dispatcher->setDefaultNamespace('Test\\Controllers'); //Bind the EventsManager to the Dispatcher $dispatcher->setEventsManager($eventsManager); return $dispatcher; }); $di->setShared('router', function () { $router = new \Phalcon\Mvc\Router(); include __DIR__ . '/routes_api.php'; $router->mount($api); return $router; });
<?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);
* Time: 17:22 */ $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'));
<?php $router = new Phalcon\Mvc\Router(false); $router->removeExtraSlashes(true); $router->notFound(array("controller" => "index", "action" => "error404")); $router->add('/', 'Index::index')->setName('main'); $router->mount(require_once COREROOT . '/app/modules/miniadmin/config/routes.php'); $router->mount(require_once COREROOT . '/app/modules/autoadmin/config/routes.php'); $router->mount(require_once COREROOT . '/app/modules/admin/config/routes.php'); return $router;