public function modulesClosure(IntegrationTester $I) { $I->wantTo('handle request and get content by using single modules strategy (closure)'); Di::reset(); $_GET['_url'] = '/login'; $di = new FactoryDefault(); $di->set('router', function () { $router = new Router(false); $router->add('/index', ['controller' => 'index', 'module' => 'frontend', 'namespace' => 'Phalcon\\Test\\Modules\\Frontend\\Controllers']); $router->add('/login', ['controller' => 'login', 'module' => 'backend', 'namespace' => 'Phalcon\\Test\\Modules\\Backend\\Controllers']); return $router; }); $application = new Application(); $view = new View(); $application->registerModules(['frontend' => function ($di) use($view) { /** @var \Phalcon\DiInterface $di */ $di->set('view', function () use($view) { $view->setViewsDir(PATH_DATA . 'modules/frontend/views/'); return $view; }); }, 'backend' => function ($di) use($view) { /** @var \Phalcon\DiInterface $di */ $di->set('view', function () use($view) { $view->setViewsDir(PATH_DATA . 'modules/backend/views/'); return $view; }); }]); $application->setDI($di); $I->assertEquals('<html>here</html>' . PHP_EOL, $application->handle()->getContent()); }
/** * registers module according to uri given * * @return bool true if module successfully loaded; <br /> * false otherwise; */ public function handle() { $boolReturn = false; $oLogger = $this->di->getFileLogger(); $oPreRouter = new Router(); $oPreRouter->add('/api(.*)', array('module' => 'api')); $oPreRouter->add('/regular(.*)', array('module' => 'regular')); $oPreRouter->add('/blind(.*)', array('module' => 'blind')); $oPreRouter->handle(); $strModuleName = $oPreRouter->getModuleName(); /** * @type Request $oRequest */ $oRequest = $this->di->getRequest(); if (array_key_exists($strModuleName, $this->knownModules)) { $this->app->registerModules(array($strModuleName => $this->knownModules[$strModuleName])); $this->app->setDefaultModule($strModuleName); $boolReturn = true; $oLogger->debug(__CLASS__ . ': ' . $oRequest->getURI() . ' leads to module: ' . $strModuleName); } else { if (!U::isLegacy()) { $strMsg = 'failed to load phalcon module'; } else { $strMsg = 'loading old backend'; } $oLogger->debug(__CLASS__ . ': ' . $strMsg . ' for "' . $oRequest->getUri() . '"'); } return $boolReturn; }
/** * @dataProvider routesProvider * * @param $arRoute * @param $strUri * @param $strClassName */ public function testRoutes($arRoute, $strUri, $strClassName) { $this->router->add($arRoute['route'], $arRoute['parameters'])->setName($arRoute['name']); $this->router->handle($strUri); $boolMatched = $this->router->wasMatched(); $this->assertTrue($boolMatched, 'failed to match ' . $strUri); $strRouteName = $this->router->getMatchedRoute()->getName(); $this->assertEquals($arRoute['name'], $strRouteName, 'matched wrong route'); $this->setUpDispatcher(); $this->dispatcher->dispatch(); $strControllerClassName = $this->dispatcher->getControllerClass(); $this->assertEquals($strClassName, $strControllerClassName, 'wrong controller class name'); }
/** * {@inheritdoc} */ public function addRoute(Route $route) { // TODO allow named parameters through options. // TODO allow usage of :controller, :action, etc. // TODO allow using prefixes if ($this->router->wasMatched()) { throw new Exception\RuntimeException('Route was already matched.'); } // Necessary for phalcon not to alter the original middleware. $middleware = $route->getMiddleware() . '\\MockController::mockAction'; $r = $this->router->add($route->getPath(), $middleware, $route->getAllowedMethods()); $r->setName($route->getName()); }
private function mvcRouter() { //Register routing $router = new Router(); $router->clear(); foreach ($this->config('route') as $url => $route) { $router->add($url, $route->toArray()); } return $router; }
/** * @param array $routes * @return MvcRouter */ public static function createFrom(array $routes) : MvcRouter { $router = new MvcRouter(false); $router->setUriSource(MvcRouter::URI_SOURCE_SERVER_REQUEST_URI); $router->removeExtraSlashes(true); foreach ($routes as $route) { $router->add($route['pattern'], $route['paths'] ?? null, $route['httpMethods'] ?? null, $route['position'] ?? MvcRouter::POSITION_LAST); } return $router; }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ protected function registerServices() { $di = new FactoryDefault(); $loader = new Loader(); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs(array(__DIR__ . '/../apps/library/'))->register(); //Registering a router $di->set('router', function () { $router = new Router(); $router->setDefaultModule("frontend"); $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2)); $router->add("/login", array('module' => 'backend', 'controller' => 'login', 'action' => 'index')); $router->add("/admin/products/:action", array('module' => 'backend', 'controller' => 'products', 'action' => 1)); $router->add("/products/:action", array('module' => 'frontend', 'controller' => 'products', 'action' => 1)); return $router; }); $this->setDI($di); }
public function addRoutes(Router $router) { $router->add('/' . $this->getApiRootUrl() . '(\\/?)', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 'Index', 'action' => 'index')); $router->addGet('/' . $this->getApiRootUrl() . '/:controller/:params', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'show', 'params' => 2)); $router->addGet('/' . $this->getApiRootUrl() . '/:controller', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'index')); $router->addPost('/' . $this->getApiRootUrl() . '/:controller', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'create')); $router->addOptions('/' . $this->getApiRootUrl() . '/:controller', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'options')); $router->addOptions('/' . $this->getApiRootUrl() . '/:controller/:params', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'options')); $router->addDelete('/' . $this->getApiRootUrl() . '/:controller/:params', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'delete', 'params' => 2)); $router->addPut('/' . $this->getApiRootUrl() . '/:controller/:params', array('namespace' => $this->getApiControllerRootNamespace(), 'controller' => 1, 'action' => 'update', 'params' => 2)); }
public function setUp() { parent::setUp(); $this->router = new Router(); $this->router->add('/api/web/v1/:controller', array('module' => 'api', 'action' => 'index', 'controller' => 1))->setName('single_controller'); $this->router->add('/api/web/v{version}', array('module' => 'api', 'action' => 'index', 'controller' => 'index'))->setName('simple_version'); $this->router->add('/api/web/v{major:[0-9]{1,2}}\\.{minor:[0-9]{1,2}}', array('module' => 'api', 'action' => 'index', 'controller' => 'index'))->setName('syntax_version'); $this->router->add('/api/web/v{major:[0-9]{1,2}}\\.{minor:[0-9]{1,2}}/:controller/:action', array('module' => 'api', 'action' => 4, 'controller' => 3))->setName('syntax_version_action_controller'); $this->router->add('/api/mobile/v{major:[0-9]{1,2}}\\.{minor:[0-9]{1,2}}/:controller/:action/:params', array('module' => 'api', 'action' => 4, 'controller' => 3, 'params' => 5))->setName('syntax_version_action_controller_parameters'); $this->router->add('/api/mobile/v{major:[0-9]{1,2}}\\.{minor:[0-9]{1,2}}/:controller/:action/:int', array('module' => 'api', 'action' => 4, 'controller' => 3, 'id' => 5))->setName('syntax_version_action_controller_id'); $this->router->add('/api/{media}/v{major:[0-9]{1,2}}\\.{minor:[0-9]{1,2}}/:controller/:action/:int', array('module' => 'api', 'action' => 5, 'controller' => 4, 'id' => 6))->setName('media_syntax_version_action_controller_id'); $arRoutes = $this->router->getRoutes(); /** * @type Router\Route $oRoute */ foreach ($arRoutes as $oRoute) { $this->routes[] = $oRoute->getPattern(); } }
/** * Sets the environment */ public function setUp() { parent::setUp(); $this->di->set('router', function () { $router = new PhRouter(false); $router->add('/admin/:controller/p/:action', array('controller' => 1, 'action' => 2))->setName('adminProducts'); $router->add('/api/classes/{class}')->setName('classApi'); $router->add('/{year}/{month}/{title}')->setName('blogPost'); $router->add('/wiki/{article:[a-z]+}')->setName('wikipedia'); $router->add('/news/{country:[a-z]{2}}/([a-z+])/([a-z\\-+])/{page}', array('section' => 2, 'article' => 3))->setName('news'); $router->add('/([a-z]{2})/([a-zA-Z0-9_-]+)(/|)', array('lang' => 1, 'module' => 'main', 'controller' => 2, 'action' => 'index'))->setName('lang-controller'); return $router; }); }
public function add($pattern, $paths = null, $httpMethods = null) { $_route = $pattern; $name = null; if ($_route instanceof \Phalcon\Mvc\Router\Route) { $pattern = $_route->getPattern(); $paths = $_route->getPaths(); $httpMethods = $_route->getHttpMethods(); $name = $_route->getName(); } if ($httpMethods != null && $name != null) { return parent::add($pattern, $paths)->via($httpMethods)->setName($name); } elseif ($httpMethods != null) { return parent::add($pattern, $paths)->via($httpMethods); } elseif ($name != null) { return parent::add($pattern, $paths)->setName($name); } else { return parent::add($pattern, $paths); } }
/** * @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; }
/** * add a new route * * @param PhRouter $router * @param $route * @param $params * @return PhRouter\RouteInterface */ public function addRoute(PhRouter &$router, $route, $params) { $route = $router->add($route, $params); return $route; }
/** * 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(); $router->add("/:controller/:action/:params", array('controller' => 1, 'action' => 2, 'params' => 3)); return $router;
<?php /** * routers.php Class * @author Phan Nguyen. * @email: phannguyen2020@gmail.com * @category: framework */ use Phalcon\Mvc\Router; $router = new Router(); $router->setDefaultModule('site'); //$router->setDefaultNamespace('Modules\Site\Controllers'); $router->add('/admin', array('module' => 'admin')); $router->add('/admin/:controller/:action/:params', array('module' => 'admin', 'controller' => 1, 'action' => 2, 'params' => 3)); $router->add('/admin/:controller', array('module' => 'admin', 'controller' => 1)); $router->add('/admin/logout', array('module' => 'admin', 'controller' => 'index', 'action' => 'logout')); //$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $router->removeExtraSlashes(true); return $router;
<?php use Phalcon\Mvc\Router; /** * The global router of the application. * * @author Xie Haozhe <*****@*****.**> */ $router = new Router(); /* Routers for DefaultController */ $router->add('/', array('controller' => 'default', 'action' => 'index')); $router->add('/getCsrfToken.action', array('controller' => 'default', 'action' => 'getCsrfToken')); $router->add('/terms', array('controller' => 'default', 'action' => 'terms')); $router->add('/privacy', array('controller' => 'default', 'action' => 'privacy')); $router->add('/changeLanguage.action', array('controller' => 'default', 'action' => 'changeLanguage')); /* Routers for ErrorsController */ $router->add('/not-supported', array('controller' => 'errors', 'action' => 'notSupportedError')); /* Routers for AccountsController */ $router->add('/accounts/signin', array('controller' => 'accounts', 'action' => 'signIn')); $router->add('/accounts/signin.action', array('controller' => 'accounts', 'action' => 'doSignIn')); $router->add('/accounts/signup', array('controller' => 'accounts', 'action' => 'signUp')); $router->add('/accounts/signup.action', array('controller' => 'accounts', 'action' => 'doSignUp')); $router->add('/accounts/verify-email', array('controller' => 'accounts', 'action' => 'verifyEmail')); $router->add('/accounts/signout', array('controller' => 'accounts', 'action' => 'signOut')); $router->add('/accounts/reset-password', array('controller' => 'accounts', 'action' => 'resetPassword')); $router->add('/accounts/forgotPassword.action', array('controller' => 'accounts', 'action' => 'doForgotPassword')); $router->add('/accounts/resetPassword.action', array('controller' => 'accounts', 'action' => 'doResetPassword')); $router->add('/user/{uid}', array('controller' => 'accounts', 'action' => 'user')); $router->add('/user/{uid}/getIssues.action', array('controller' => 'accounts', 'action' => 'getIssues')); /* Routers for DashboardController */ $router->add('/dashboard', array('controller' => 'dashboard', 'action' => 'index'));
<?php use Phalcon\Mvc\Router; $router = new Router(); $router->add('/', ['controller' => 'index', 'action' => 'index']); $router->add('/tokencheck', ['controller' => 'index', 'action' => 'tokencheck']); $router->handle();
+------------------------------------------------------------------------+ | Copyright (c) 2013-2016 Phalcon Team and contributors | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | | | | If you did not receive a copy of the license and are unable to | | obtain it through the world-wide-web, please send an email | | to license@phalconphp.com so we can send you a copy immediately. | +------------------------------------------------------------------------+ */ use Phalcon\Mvc\Router; use Phosphorum\Http\Filter\Ajax; $router = new Router(false); $router->removeExtraSlashes(true); $router->add('/help/stats', ['controller' => 'help', 'action' => 'stats']); $router->add('/help/about', ['controller' => 'help', 'action' => 'about']); $router->add('/help/moderators', ['controller' => 'help', 'action' => 'moderators']); $router->add('/help/voting', ['controller' => 'help', 'action' => 'voting']); $router->add('/help/markdown', ['controller' => 'help', 'action' => 'markdown']); $router->add('/help/karma', ['controller' => 'help', 'action' => 'karma']); $router->add('/help/badges', ['controller' => 'help', 'action' => 'badges']); $router->add('/help/create-post', ['controller' => 'help', 'action' => 'create']); $router->add('/help', ['controller' => 'help', 'action' => 'index']); $router->add('/discussions', ['controller' => 'discussions', 'action' => 'index']); $router->add('/hook/mail-bounce', ['controller' => 'hooks', 'action' => 'mailBounce']); $router->add('/hook/mail-reply', ['controller' => 'hooks', 'action' => 'mailReply']); $router->add('/search', ['controller' => 'discussions', 'action' => 'search']); $router->addPost('/preview', ['controller' => 'utils', 'action' => 'preview']); $router->add('/reply/accept/{id:[0-9]+}', ['controller' => 'replies', 'action' => 'accept']); $router->add('/reply/vote-up/{id:[0-9]+}', ['controller' => 'replies', 'action' => 'voteUp']);
<?php $basedomain = $_SERVER['SERVER_NAME']; //apply rules if you need to tweak your basedomain use Phalcon\Mvc\Router; $router = new Router(); $router->removeExtraSlashes(true); /* * Business routes */ $router->add("/", array('module' => 'business', 'controller' => 'index', 'action' => 'index'))->setHostName('business.' . $basedomain); $router->add("/:controller", array('module' => 'business', 'controller' => 1, 'action' => 'index'))->setHostName('business.' . $basedomain); $router->add("/:controller/:action", array('module' => 'business', 'controller' => 1, 'action' => 2))->setHostName('business.' . $basedomain); $router->add("/:controller/:action/:params", array('module' => 'business', 'controller' => 1, 'action' => 2, 'params' => 3))->setHostName('business.' . $basedomain); /* * API routes */ $router->add("/", array('module' => 'api', 'controller' => 'index', 'action' => 'index'))->setHostName('api.' . $basedomain); $router->add("/{version}", array('module' => 'api', 'controller' => 'index', 'action' => 'index'))->setHostName('api.' . $basedomain); $router->add("/{version}/:controller", array('module' => 'api', 'controller' => 2, 'action' => 'index'))->setHostName('api.' . $basedomain); $router->add("/{version}/:controller/:action", array('module' => 'api', 'controller' => 2, 'action' => 3))->setHostName('api.' . $basedomain); $router->add("/{version}/:controller/:action/{params}", array('module' => 'api', 'controller' => 2, 'action' => 3))->setHostName('api.' . $basedomain); return $router;
public function diRouter() { $di = $this->getDI(); $cachePrefix = $this->getAppName(); $cacheFile = $this->getConfigPath() . "/_cache.{$cachePrefix}.router.php"; if ($router = $this->readCache($cacheFile, true)) { return $router; } $moduleManager = $di->getModuleManager(); $config = new Config(); $moduleName = ''; if ($moduleManager && ($modulesArray = $moduleManager->getModules())) { foreach ($modulesArray as $moduleName => $module) { //NOTICE: EvaEngine Load front-end router at last $config->merge(new Config($moduleManager->getModuleRoutesFrontend($moduleName))); $config->merge(new Config($moduleManager->getModuleRoutesBackend($moduleName))); } } //Disable default router $router = new Router(false); //Last extra slash $router->removeExtraSlashes(true); //Set last module as default module $router->setDefaultModule($moduleName); //NOTICE: Set a strange controller here to make router not match default index/index $router->setDefaultController('EvaEngineDefaultController'); $config = $config->toArray(); foreach ($config as $url => $route) { if (count($route) !== count($route, COUNT_RECURSIVE)) { if (isset($route['pattern']) && isset($route['paths'])) { $method = isset($route['httpMethods']) ? $route['httpMethods'] : null; $router->add($route['pattern'], $route['paths'], $method); } else { throw new Exception\RuntimeException(sprintf('No route pattern and paths found by route %s', $url)); } } else { $router->add($url, $route); } } if (!$di->getConfig()->debug) { $this->writeCache($cacheFile, $router, true); } else { //Dump merged routers for debug $this->writeCache($this->getConfigPath() . "/_debug.{$cachePrefix}.router.php", $router, true); } return $router; }
* The FactoryDefault Dependency Injector automatically registers the right services to provide a full stack framework */ $di = new FactoryDefault(); /** * Registering a router */ $di->set('router', function () { $router = new Router(); /** * 默认命名空间 */ $router->setDefaultModule('frontend'); /** * 后台路径 */ $router->add('/' . ADMIN . '/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_]+)(\\/?\\S+)*', ['module' => 'backend', 'controller' => 1, 'action' => 2, 'params' => 3]); /** * 首页 */ $router->add('/', ['controller' => 'index', 'action' => 'index']); //全局搜索页面 $router->add('/index/search_{key:\\s+}', array('controller' => 'index', 'action' => 'search', 'params' => 2)); //学校索引页面 $router->add('/school', array('controller' => 'school', 'action' => 'search', 'params' => 2)); //大学主页 $router->add('/college/detail_{id:\\d+}\\.html', array('controller' => 'college', 'action' => 'index', 'params' => 2)); //大学教师 $router->add('/college/teacher_{id:\\d+}\\.html', array('controller' => 'college', 'action' => 'teacher', 'params' => 2)); //历年分数 $router->add('/college/grade_{id:\\d+}\\.html', array('controller' => 'college', 'action' => 'grade', 'params' => 2)); //大学专业
<?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'));
$di->set('router', function () use($di) { $router = new Router(); $router->add("/", "Index::index"); $router->add("/history/save", "History::saveHistory"); $router->add("/history/show", "History::showHistory"); $router->handle(); return $router; }); $di->set('url', function () { $url = new UrlProvider(); $url->setBaseUri('/'); return $url; }); // Настраиваем сервис для работы с БД $di->set('db', function () use($config) { return new Phalcon\Db\Adapter\Pdo\Mysql(array('host' => $config->database->host, 'username' => $config->database->username, 'password' => $config->database->password, 'dbname' => $config->database->dbname)); }); $di->set('router', function () use($di) { $router = new Router(); $router->add("/", "Index::index"); $router->add("/history/save", "History::saveHistory"); $router->add("/history/show", "History::showHistory"); $router->handle(); return $router; }); // Обработка запроса $application = new Phalcon\Mvc\Application($di); echo $application->handle()->getContent(); } catch (Phalcon\Exception $e) { echo "PhalconException: ", $e->getMessage(); }
<?php use Phalcon\Mvc\Router; $router = new Router(); //$router = new Router(false); $router->removeExtraSlashes(true); // 移除URL多余的斜杠 //$router->setUriSource(Router::URI_SOURCE_GET_URL); $router->setDefaultController('index'); $router->setDefaultAction('index'); $router->setDefaultModule('frontend'); $router->setDefaultNamespace('Apps\\Frontend\\Controllers'); // APP API $router->add('/appApi', ['module' => 'appApi', 'namespace' => 'Apps\\AppApi\\Controllers\\', 'controller' => 'index', 'action' => 'index']); $router->add('/appApi' . '/:action', ['module' => 'appApi', 'namespace' => 'Apps\\AppApi\\Controllers\\', 'controller' => 'index', 'action' => 1]); $defaultController = 'index'; $defaultAction = 'index'; $urlBase = ['/backend' => ['module' => 'backend', 'namespace' => 'Apps\\Backend\\Controllers\\'], '/manage' => ['module' => 'manage', 'namespace' => 'Apps\\Manage\\Controllers\\'], '/frontend' => ['module' => 'frontend', 'namespace' => 'Apps\\Frontend\\Controllers\\'], '/pay' => ['module' => 'pay', 'namespace' => 'Apps\\Pay\\Controllers\\']]; foreach ($urlBase as $k => $v) { $module = $v['module']; $namespace = $v['namespace']; $router->add($k . '/:params', ['module' => $module, 'namespace' => $namespace, 'controller' => $defaultController, 'action' => $defaultAction, 'params' => 1]); $router->add($k . '/:controller/:params', ['module' => $module, 'namespace' => $namespace, 'controller' => 1, 'action' => $defaultAction, 'params' => 2]); $router->add($k . '/:controller/:action/:params', ['module' => $module, 'namespace' => $namespace, 'controller' => 1, 'action' => 2, 'params' => 3]); } return $router;
$router->mount($frontend); /** * Define routes for each module */ $modules = ['oauth', 'backend']; foreach ($modules as $module) { $group = new Group(['module' => $module]); $group->setPrefix('/' . $module); $group->add('/:controller/:action/:params', array('controller' => 1, 'action' => 2, 'params' => 3)); $group->add('/:controller/:int', ['controller' => 1, 'id' => 2]); $group->add('/:controller[/]?', ['controller' => 1]); $frontend->add('/:controller/:int/{slug:[a-z\\-]+}', ['controller' => 1, 'id' => 2, 'slug' => 3, 'action' => 'view']); $group->add('[/]?', array()); $router->mount($group); } $router->add('/backend', ['module' => 'backend', 'controller' => 'dashboard']); $router->add('/oauth/github/access_token', ['module' => 'oauth', 'controller' => 'login', 'action' => 'tokenGithub']); $router->add('/oauth/google/access_token', ['module' => 'oauth', 'controller' => 'login', 'action' => 'tokenGoogle']); $router->add('/oauth/facebook/access_token', ['module' => 'oauth', 'controller' => 'login', 'action' => 'tokenFacebook']); $router->add('/questions', ['module' => 'frontend', 'controller' => 'posts']); /** * @link https://docs.phalconphp.com/en/latest/reference/routing.html#match-callbacks */ $router->add('/{router}', ['module' => 'frontend', 'controller' => 'router'])->beforeMatch(function ($uri, $route) { if ($uri == '/questions') { return false; } if ($uri == '/backend') { return false; } if ($uri == '/') {
* Copyright 2015 Eduardo Pereira <*****@*****.**>. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ use Phalcon\Mvc\Router; // Instantiate our Router object $router = new Router(); /** * Start creating routes */ $router->add("/", array('controller' => 'main', 'action' => 'default')); /** * Return our router object, it's called in services.php */ 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 /** * 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();
use Phalcon\Logger; use Phalcon\Events\Manager as EventsManager; use Phalcon\Logger\Adapter\File as FileLogger; try { // Register an autoloader $loader = new Loader(); // $loader->registerDirs(array( // '../app/controllers/', // '../app/models/' // ))->register(); // Create a DI $di = new FactoryDefault(); $di->set('router', function () { $router = new Router(); $router->setDefaultModule("frontend"); $router->add('/:controller/:action', array('module' => 'frontend', 'controller' => 1, 'action' => 2)); $router->add("/login", array('module' => 'backend', 'controller' => 'login', 'action' => 'index')); $router->add('/admin/:controller/:action', array('module' => 'backend', 'controller' => 1, 'action' => 2)); return $router; }); // Setup the view component // $di->set('view', function () { // $view = new View(); // $view->setViewsDir('../app/views/'); // return $view; // }); $di['db'] = function () { //$eventsManager = new EventsManager(); // //$logger = new FileLogger("../apps/logs/debug.log"); // //// Listen all the database events //$eventsManager->attach('db', function ($event, $connection) use ($logger) {