/** * @return $this */ public function createDependencies() { $dependency = new FactoryDefault(); $dependency->set('db', function () { return $this->getDatabase(); }); $dependency->set('router', function () { $router = new Router(false); $routes = Routes::get(); foreach ($routes as $group => $controllers) { foreach ($controllers as $controller) { $router->add($controller['route'], ['namespace' => "App\\Controllers\\{$group}", 'controller' => $controller['class'], 'action' => 'run'], $controller['method']); } } $router->notFound(['namespace' => 'PhRest\\Controllers', 'controller' => 'Missing', 'action' => 'run']); return $router; }); $dependency->set('view', function () { return new View(); }, true); $this->setDI($dependency); return $this; }
<?php /** * Created by PhpStorm. * User: vlad * Date: 8/28/15 * Time: 10:34 PM */ use Phalcon\Mvc\Router; $router = new Router(); $router->removeExtraSlashes(true); $router->add("/", array("controller" => "projects", "action" => "index")); $router->add("/projects(/?index)?", array("controller" => "static", "action" => "error")); $router->add("/404", array("controller" => "static", "action" => "error404")); $router->add("/403", array("controller" => "static", "action" => "error403")); $router->notFound(array("controller" => "static", "action" => "error404")); $router->handle();
/** * Initializes the router * * @param array $options */ protected function initRouter($options = array()) { $config = $this->di['config']; $this->di['router'] = function () use($config) { // Create the router without default routes (false) $router = new PhRouter(true); // 404 $router->notFound(array("controller" => "index", "action" => "notFound")); $router->removeExtraSlashes(true); foreach ($config['routes'] as $route => $items) { $router->add($route, $items->params->toArray())->setName($items->name); } return $router; }; }
<?php use Phalcon\Mvc\Router; $router = new Router(); //Remove trailing slashes automatically $router->removeExtraSlashes(true); //main route $router->add("/", array('controller' => 'index', 'action' => 'index')); //GET VERB - GET ELEMENT //Get elemets of relationship. Ex: /department/2/user $router->addGet('/:controller/:int/([a-zA-Z0-9_-]+)', array('controller' => 1, 'action' => "list", 'id' => 2, 'relationship' => 3)); //Get one element. Ex: /user/2 $router->addGet('/:controller/:int', array('controller' => 1, 'action' => "get", 'id' => 2)); //Get all elements. Ex: /user $router->addGet('/:controller', array('controller' => 1, 'action' => "list")); //POST VERB - CREATE ELEMENT //Create a new element. Ex: /user $router->addPost('/:controller', array('controller' => 1, 'action' => "save")); //PUT VERB - UPDATE ELEMENT //Update a new element. Ex: /user $router->addPut('/:controller/:int', array('controller' => 1, 'action' => "save", 'id' => 2)); //DELETE VERB - UPDATE ELEMENT //Update a new element. Ex: /user $router->addDelete('/:controller/:int', array('controller' => 1, 'action' => "delete", 'id' => 2)); //not founded route $router->notFound(array('controller' => 'error', 'action' => 'page404')); $router->setDefaults(array('controller' => 'index', 'action' => 'index')); return $router;
<?php use Phalcon\Mvc\Router, Phalcon\Mvc\Router\Group as RouterGroup; $router = new Router(false); $router->setDefaultModule(SITENAME); $router->removeExtraSlashes(true); $router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); $router->add('/', array('controller' => 'Index', 'action' => 'index'))->setName('homepage'); $router->add('/testMenu/{store_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Index', 'action' => 'testMenu'))->setName('homepage-test'); $router->add('/menu/ajax/{store_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Index', 'action' => 'menuAjax'))->setName('homepage-ajax'); /* Orders */ $router->add('/order/{order_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Order', 'action' => 'index'))->setName('order'); $router->add('/order/{order_id:[a-z0-9\\-_A-Z]+}/{drink_id:[a-z0-9\\-_A-Z]+}/{coldheat_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Order', 'action' => 'orderDrink'))->setName('order-drink'); $router->add('/order/{order_id:[a-z0-9\\-_A-Z]+}/overview', array('controller' => 'Order', 'action' => 'orderOverview'))->setName('order'); $router->add('/order/add-drink', array('controller' => 'Order', 'action' => 'addDrink'))->setName('order-add-drink'); /* resouces */ $router->add('/resource/stores', array('controller' => 'Resource', 'action' => 'stores'))->setName('resouce-stores'); $router->add('/resource/oStore/{order_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Resource', 'action' => 'orderStore'))->setName('resouce-order-store'); $router->add('/resource/oDrink/{drink_id:[a-z0-9\\-_A-Z]+}/{coldheat_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Resource', 'action' => 'orderDrink'))->setName('resouce-drink-detail'); $router->add('/resource/oDrinkList/{order_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Resource', 'action' => 'orderDrinkList'))->setName('resouce-drink-detail'); $router->add('/order/hook/{store_id:[a-z0-9\\-_A-Z]+}', array('controller' => 'Order', 'action' => 'hookOrder'))->setName('order-hook'); $router->notFound(array('controller' => 'Index', 'action' => 'notFound'));
$router->add('/college-{pro_code:\\d+}-{gen_id:\\d+}-{public_type:\\d+}-{col_type:\\d+}-{attr_id:\\d+}-{sort:\\d+}', array('controller' => 'col', 'action' => 'list', 'params' => 8)); $router->add('/college-{pro_code:\\d+}-{gen_id:\\d+}-{public_type:\\d+}-{col_type:\\d+}-{attr_id:\\d+}\\-{sort:\\d+}-p-{page_id:\\d+}', array('controller' => 'col', 'action' => 'lists', 'params' => 9)); /** * 小初高列表 */ $router->add('/school-{pro_code:\\d+}-{city_code:\\d+}-{area_code:\\d+}-{public_type:\\d+}-{sch_type:\\d+}-{stress:\\d+}-{sort:\\d+}', array('controller' => 'sch', 'action' => 'list', 'params' => 9)); $router->add('/school-{pro_code:\\d+}-{city_code:\\d+}-{area_code:\\d+}-{public_type:\\d+}-{sch_type:\\d+}-{stress:\\d+}-{sort:\\d+}-p-{page_id:\\d+}', array('controller' => 'sch', 'action' => 'lists', 'params' => 10)); /** * 幼儿园列表 */ $router->add('/child-{pro_code:\\d+}-{city_code:\\d+}-{area_code:\\d+}-{public_type:\\d+}-{sort:\\d+}', array('controller' => 'child', 'action' => 'list', 'params' => 7)); $router->add('/child-{pro_code:\\d+}-{city_code:\\d+}-{area_code:\\d+}-{public_type:\\d+}-{sort:\\d+}-p-{page_id:\\d+}', array('controller' => 'child', 'action' => 'lists', 'params' => 8)); /** * 跳转404 */ $router->notFound(['controller' => 'about', 'action' => 'error']); return $router; }); /** * The URL component is used to generate all kinds of URLs in the application */ $di->set('url', function () { $url = new UrlResolver(); $url->setBaseUri('/'); return $url; }); /** * Starts the session the first time some component requests the session service */ $di->setShared('session', function () { $session = new SessionAdapter();
<?php use Phalcon\Mvc\Router; // Create the router $router = new Router(); $router->setDefaultNamespace('App\\Controllers'); $router->removeExtraSlashes(true); $router->add("/", array('namespace' => 'App\\Controllers', "controller" => "index", "action" => "index"))->setName('home'); $router->add('/api/(v1|v2)/about', array('namespace' => 'App\\Controllers\\api\\v1', 'controller' => 'about', 'version' => 1))->setName('about'); $router->notFound(array("controller" => "index", "action" => "route404")); $router->handle();
<?php use Phalcon\Mvc\Router; // Create the router $router = new Router(); $router->removeExtraSlashes(true); $router->setDefaultNamespace(ucfirst(BASE_NAME) . '\\Controllers'); $router->setDefaultController('index'); $router->setDefaultAction('index'); $router->notFound(array('controller' => 'test', 'action' => 'show404')); $router->handle(); return $router;
<?php use Phalcon\Mvc\Router; // Create the router $router = new Router(); // Define a route $router->add(':controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); // 默认 $router->add('/', array('controller' => 'index', 'action' => 'index')); $router->notFound(array('controller' => 'public', 'action' => 'err404')); return $router;
/** * Setting up custom Request object */ $di->set('request', function () { return new RestRequest(); }); /** * Setting up custom Dispatcher */ $di->set('dispatcher', function () { return new RestDispatcher(); }); /** * Setting up router and mounting AppRouter */ $di->set('router', function () { $router = new Router(false); $router->removeExtraSlashes(true); $router->notFound(array('controller' => 'error404')); $router->mount(new AppRouter()); return $router; }); /** * Run the application */ $app = new Application($di); $app->useImplicitView(false); $app->handle()->send(); } catch (Exception $e) { echo 'Uncaught Exception: ' . get_class($e) . $e->getMessage(); }
}); $dispatcher = new \Phalcon\Mvc\Dispatcher(); $dispatcher->setEventsManager($evManager); return $dispatcher; }), 'modelsManager' => array('class' => function ($application) { $eventsManager = $application->getDI()->get('eventsManager'); $modelsManager = new \Phalcon\Mvc\Model\Manager(); $modelsManager->setEventsManager($eventsManager); $eventsManager->attach('modelsManager', new \Engine\Db\Model\Annotations\Initializer()); return $modelsManager; }), 'router' => array('class' => function ($application) { $router = new Router(false); $router->add('/', array('module' => 'frontend', 'controller' => 'index', 'action' => 'index'))->setName('default'); foreach ($application->getModules() as $key => $module) { $router->add('/' . $key . '/:params', array('module' => $key, 'controller' => 'index', 'action' => 'index', 'params' => 1))->setName($key); $router->add('/' . $key . '/:controller/:params', array('module' => $key, 'controller' => 1, 'action' => 'index', 'params' => 2)); $router->add('/' . $key . '/:controller/:action/:params', array('module' => $key, 'controller' => 1, 'action' => 2, 'params' => 3)); } $router->add('/catalog/category/{id:([0-9]{1,11})}/:params', array('module' => 'catalog', 'controller' => 'index', 'action' => 'category', 'id' => 1))->setName('catalog-category'); $router->add('/catalog/product/{id:([0-9]{1,11})}/:params', array('module' => 'catalog', 'controller' => 'product', 'action' => 'index', 'id' => 1))->setName('catalog-product-view'); $router->add('/oauth/login/{id:([a-z]{1,20})}/:params', array('module' => 'oauth', 'controller' => 'index', 'action' => 'index'))->setName('oauth-index-index'); $router->add('/api/users/{id:([0-9]{1,32})}/:params', array('module' => 'api', 'controller' => 'users', 'action' => 'get')); $router->add('/user/{id:([0-9]{1,32})}/:params', array('module' => 'user', 'controller' => 'index', 'action' => 'view'))->setName('user-index-view'); $router->add('/frontend/index/getting-started/:params', array('module' => 'frontend', 'controller' => 'index', 'action' => 'gettingStarted')); $router->notFound(array('module' => 'frontend', 'namespace' => 'Frontend\\Controller', 'controller' => 'index', 'action' => 'index')); return $router; }, 'parameters' => array('uriSource' => Router::URI_SOURCE_SERVER_REQUEST_URI)), 'view' => array('class' => function () { $class = new View(); $class->registerEngines(array('.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php')); return $class; }, 'parameters' => array('layoutsDir' => APPLICATION_PATH . '/layouts/')), 'auth' => array('class' => '\\App\\Service\\Auth')), 'application' => array('modules' => array('frontend' => array('className' => 'Frontend\\Module', 'path' => APPLICATION_PATH . '/modules/frontend/Module.php'), 'catalog' => array('className' => 'Catalog\\Module', 'path' => APPLICATION_PATH . '/modules/catalog/Module.php'), 'admin' => array('className' => 'Admin\\Module', 'path' => APPLICATION_PATH . '/modules/admin/Module.php'), 'api' => array('className' => 'Api\\Module', 'path' => APPLICATION_PATH . '/modules/api/Module.php'), 'user' => array('className' => 'User\\Module', 'path' => APPLICATION_PATH . '/modules/user/Module.php'), 'oauth' => array('className' => 'OAuth\\Module', 'path' => APPLICATION_PATH . '/modules/oauth/Module.php'))));
<?php use Phalcon\Mvc\Router; use Phalcon\Mvc\Application; // Create the router $router = new Router(); $router->setDefaultModule("Frontend"); // fronted $router->add(':controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); // 多模块 foreach ($application->getModules() as $key => $module) { $router->add('/' . $key . '/:params', array('module' => $key, 'controller' => 'index', 'action' => 'index', 'params' => 1))->setName($key); $router->add('/' . $key . '/:controller/:params', array('module' => $key, 'controller' => 1, 'action' => 'index', 'params' => 2)); $router->add('/' . $key . '/:controller/:action/:params', array('module' => $key, 'controller' => 1, 'action' => 2, 'params' => 3)); } // default $router->add('/', array('controller' => 'index', 'action' => 'index', 'module' => 'Frontend')); $router->notFound(array('namespace' => 'Frontend\\Controller', 'module' => 'Frontend', 'controller' => 'public', 'action' => 'err404')); return $router;
<?php use Phalcon\Mvc\Router; $router = new Router(); //路由 $router->add('/', array('namespace' => 'Index\\Controllers', 'controller' => 'Index', 'action' => 'index')); $router->add('/(index|Index)/:controller/:action/:params', array('namespace' => 'Index\\Controllers', 'controller' => 2, 'action' => 3, 'params' => 4)); $router->add('/(App|app)/:controller/:action/:params', array('namespace' => 'App\\Controllers', 'controller' => 2, 'action' => 3, 'params' => 4)); $router->add('/(Agent|agent)/:controller/:action/:params', array('namespace' => 'Agent\\Controllers', 'controller' => 2, 'action' => 3, 'params' => 4)); $router->notFound(array("namespace" => 'System\\Controllers', "controller" => "Error", "action" => "error404")); $router->setDefaults(array('namespace' => 'Index\\Controllers', 'controller' => 'Index', 'action' => 'index')); return $router;
/** * Initializes the router * * @param array $options */ protected function initRouter($options = array()) { $config = $this->di['config']; $this->di['router'] = function () use($config) { $router = new Router(false); $router->notFound(array('controller' => 'index', 'action' => 'notFound')); $router->removeExtraSlashes(true); foreach ($config['routes']->toArray() as $route => $items) { $route = $router->add($route, $items['params']); if (isset($items['name'])) { $route->setName($items['name']); } if (isset($items['via'])) { $route->via($items['via']); } if (isset($items['hostname'])) { $route->setHostname($items['hostname']); } } return $router; }; }
<?php use Phalcon\Mvc\Router; use Libraries\RouteLoader\RouteLoader; // Create the router $router = new Router(false); // Get all available routes located in the routes dir recursively $routeLoader = new RouteLoader($config->application->routesDir); // Mount all available routes foreach ($routeLoader->findRoutes() as $route) { $router->mount(new $route()); } //Set 404 paths $router->notFound(array('namespace' => 'Controllers\\BaseControllers', 'controller' => 'error', 'action' => 'send404')); // Return the router return $router;
public function notFound($paths) { return parent::notFound($paths); }