private function setUpDispatcher() { $this->dispatcher->setControllerName($this->router->getControllerName()); $this->dispatcher->setActionName($this->router->getActionName()); $this->dispatcher->setParams($this->router->getParams()); $oDispatcherEventManager = new Manager(); $oDispatcherEventManager->attach('dispatch:beforeDispatch', function (Event $oEvent, Dispatcher $oDispatcher, $data) { return false; }); $this->dispatcher->setEventsManager($oDispatcherEventManager); }
function setDi() { $di = new FactoryDefault(); $di['router'] = function () use($di) { $router = new Router(); $router->setDefaultModule('mobimall'); return $router; }; $di['url'] = function () { $url = new UrlResolver(); $url->setBaseUri('/'); return $url; }; $di['session'] = function () { $session = new SessionAdapter(); // $session->start(); return $session; }; $loader = new Loader(); $loader->registerNamespaces(array('Mall\\Mdu' => __DIR__ . '/../apps/mdu')); $sysConfig = (include __DIR__ . '/../config/sysconfig.php'); $di['sysconfig'] = function () use($sysConfig) { return $sysConfig; }; $loader->register(); return $di; }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ public function registerServices() { $di = new FactoryDefault(); $loader = new Loader(); $namespaces = []; $map = (require_once __DIR__ . '/../autoload_namespaces.php'); foreach ($map as $k => $values) { $k = trim($k, '\\'); if (!isset($namespaces[$k])) { $dir = '/' . str_replace('\\', '/', $k) . '/'; $namespaces[$k] = implode($dir . ';', $values) . $dir; } } $loader->registerNamespaces($namespaces); $loader->register(); /** * Register a router */ $di->set('router', function () { $router = new Router(); $router->setDefaultModule('frontend'); //set frontend routes $router->mount(new FrontendRoutes()); // return $router; }); $this->setDI($di); }
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()); }
protected function initRouter() { $this->getDI()->set('router', function () { $router = new Router(); $router->setDefaultModule('hrm'); return $router; }); }
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; }
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 handle($path = null) { if ($path === null) { $path = $this->getRewriteUri(); } // First attempt regular resolution. parent::handle($path); $router_route = $this->getMatchedRoute(); if ($router_route !== NULL) { return $path; } $di = $this->getDI(); $module_list = array_keys($di->get('phalcon_modules')); $path = trim($path, self::URI_DELIMITER); if ($path != '') { $path = explode(self::URI_DELIMITER, $path); if (in_array($path[0], $module_list)) { $this->_module = array_shift($path); } if (count($path) && !empty($path[0])) { $this->_controller = array_shift($path); } if (count($path) && !empty($path[0])) { $this->_action = array_shift($path); } if (count($path)) { $this->_params = $path; } } return $path; }
public function __construct() { parent::__construct(false); $this->add('/admin', ['module' => 'backend', 'controller' => 'index', 'action' => 'index']); $this->add('/index', ['module' => 'frontend', 'controller' => 'index', 'action' => 'index']); $this->add('/', ['module' => 'frontend', 'controller' => 'index', 'action' => 'index']); }
/** * Register the services here to make them general or register in the ModuleDefinition to make them module-specific */ protected function registerServices() { $di = new FactoryDefault(); require '../../../autoloader.php'; $loader = new Loader(); /** * We're a registering a set of directories taken from the configuration file */ $loader->registerDirs(array(__DIR__ . '/../apps/library/'))->register(); $router = new Router(); $router->setDefaultModule("frontend"); //Registering a router $di->set('router', function () use($router) { return $router; }); $this->setDI($di); }
/** * Load frontend router * * @param \Phalcon\Mvc\Router $router * @return \Phalcon\Mvc\Router */ function zcms_load_frontend_router($router) { //Get frontend module $frontendModule = get_child_folder(APP_DIR . '/frontend/'); $frontendModule = array_reverse($frontendModule); foreach ($frontendModule as $module) { $routerClass = 'Router' . ucfirst($module); $fileRoute = APP_DIR . "/frontend/{$module}/{$routerClass}.php"; if (file_exists($fileRoute)) { require_once $fileRoute; if (class_exists($routerClass)) { $router->mount(new $routerClass()); } } } return $router; }
public function __construct() { parent::__construct(); $this->setDefaultController('index'); $this->setDefaultAction('index'); $this->add('/:module/:controller/:action/:params', ['module' => 1, 'controller' => 2, 'action' => 3, 'params' => 4])->setName('default'); $this->add('/:module/:controller', ['module' => 1, 'controller' => 2, 'action' => 'index'])->setName('default_action'); $this->add('/:module', ['module' => 1, 'controller' => 'index', 'action' => 'index'])->setName('default_controller'); }
/** * @param null $uri */ public function handle($uri = null) { parent::handle($uri); if ($this->_wasMatched) { $afterMatch = $this->_matchedRoute->getAfterMatch(); if (is_callable($afterMatch)) { call_user_func($afterMatch, $uri, $this->_matchedRoute); } } }
public function handle($strUrl = null) { /** * @type \DiCustom $di */ $di = Di::getDefault(); $oLogger = $di->getFileLogger(); $oLogger->debug('handle worked in api module for "' . $strUrl . '"'); parent::handle($strUrl); }
public function __construct(DiInterface $di) { parent::__construct(false); $this->clear(); $this->removeExtraSlashes(true); $this->setUriSource(PhalconRouter::URI_SOURCE_SERVER_REQUEST_URI); $this->setDefaultAction('index'); $this->setDefaultController('index'); $this->setDI($di); $this->notFound(['controller' => 'error', 'action' => 'not-found']); }
/** * @param Router\Route $route * @return array */ private function collectParams(Router\Route $route) { $matches = $this->router->getMatches(); $params = []; foreach ($route->getPaths() as $name => $position) { if (isset($matches[$position])) { $params[$name] = $matches[$position]; } } return $params; }
/** * 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; }); }
/** * 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; }
public function __construct() { parent::__construct(); $this->setDefaultController('index'); $this->setDefaultAction('index'); // $this->setDefaultModule("admin"); $this->add('/:module/:controller/:action/:params', ['module' => 1, 'controller' => 2, 'action' => 3, 'params' => 4])->setName('default'); $this->add('/:module/:controller', ['module' => 1, 'controller' => 2, 'action' => 'index'])->setName('default_action'); $this->add('/:module', ['module' => 1, 'controller' => 'index', 'action' => 'index'])->setName('default_controller'); //暂时使用后台管理当首页 $this->add('/', ['module' => 'admin', 'controller' => 'index', 'action' => 'index'])->setName('default_controller'); $this->removeExtraSlashes(true); }
public function routing(Router $router) { $this->setModuleName($router->getModuleName()); $this->setNamespaceName($router->getNamespaceName()); $this->setControllerName($router->getControllerName()); $this->setActionName($router->getActionName()); $this->setParams($router->getParams()); }
/** * @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; }
public function __construct() { parent::__construct(false); static::$runningUnitTest = Config::runningUnitTest(); // @codeCoverageIgnoreStart if ($this->_sitePathPrefix = Config::get('app.site_path')) { $this->_uriSource = self::URI_SOURCE_GET_URL; $this->_sitePathLength = strlen($this->_sitePathPrefix); } // @codeCoverageIgnoreEnd $this->removeExtraSlashes(true); $routes = is_file($file = $_SERVER['PHWOOLCON_ROOT_PATH'] . '/app/routes.php') ? include $file : []; is_array($routes) and $this->addRoutes($routes); $this->cookies = static::$di->getShared('cookies'); $this->response = static::$di->getShared('response'); $this->response->setStatusCode(200); }
public function testAnnotations() { $di = new Di(); $di['request'] = new \Phalcon\Http\Request(); $router = new Router(false); $router->setDI($di); $loader = new ArrayRouteLoader($router); $loader->load(include __DIR__ . '/Fixtures/routes.php'); $router->handle(); $this->assertCount(3, $router->getRoutes()); $routes = [['uri' => '/test4', 'method' => 'GET', 'controller' => 'test4', 'action' => 'test4'], ['uri' => '/test4', 'method' => 'POST', 'controller' => 'test4', 'action' => 'test4'], ['uri' => '/test5', 'method' => 'POST', 'controller' => 'test5', 'action' => 'test5'], ['uri' => '/test6', 'method' => 'GET', 'controller' => 'test6', 'action' => 'test6']]; foreach ($routes as $route) { $_SERVER['REQUEST_METHOD'] = $route['method']; $router->handle($route['uri']); $this->assertEquals($router->getControllerName(), $route['controller']); $this->assertEquals($router->getActionName(), $route['action']); $this->assertEquals($router->isExactControllerName(), true); } }
public function handle($uri = null) { if (empty($uri)) { $uri = $this->getRewriteUri(); } $di = $this->getDI(); $routeManager = $di->get('databaseRouteManager'); $routes = $di->get('config')->get('routes', false); $route = $routeManager->findByUrl($uri); if ($route && $routes) { $routeParams = $routes->get($route->name, false); if ($routeParams) { $this->add($uri, ['module' => $routeParams->module, 'controller' => $routeParams->controller, 'action' => $routeParams->action, 0 => substr($uri, 1)]); } else { throw new \Exception('Database route not configured'); } } return parent::handle($uri); }
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); } }
public function __construct() { parent::__construct(); $session = \Phalcon\DI::getDefault()->getSession(); //Set the default namespace for all controllers that doesn't match our custom routes //e.g '/auth/login' will route to something like 'MyApp\Controllers\AuthController::loginAction()' $this->setDefaultNamespace('PRIME\\Controllers\\'); //Our custom routes will not use slashes at the of the URIs $this->removeExtraSlashes(true); if ($session->has("auth")) { //Retrieve its value $auth = $session->get("auth"); $this->theme = $auth['theme']; $directory = '../app/themes/' . $this->theme . '/widgets/'; //get all files in specified directory $files = glob($directory . "*", GLOB_ONLYDIR); $files = array_map('basename', $files); $this->themeLevel2SetupNamespacedRoutes("widgets", $files); $directory = '../app/authenticators/'; //get all files in specified directory $files = glob($directory . "*", GLOB_ONLYDIR); $files = array_map('basename', $files); $this->level2SetupNamespacedRoutes("authenticators", $files); $directory = '../app/form_elements/'; //get all files in specified directory $files = glob($directory . "*", GLOB_ONLYDIR); $files = array_map('basename', $files); $this->level2SetupNamespacedRoutes("form_elements", $files); $directory = '../app/data_connectors/'; //get all files in specified directory $files = glob($directory . "*", GLOB_ONLYDIR); $files = array_map('basename', $files); $this->level2SetupNamespacedRoutes("data_connectors", $files); $directory = '../app/themes/' . $this->theme . '/portlets/'; $this->themeLevel1SetupNamespacedRoutes("portlets"); $directory = '../app/themes/' . $this->theme . '/dashboards/'; $this->themeLevel1SetupNamespacedRoutes("dashboards"); $directory = '../app/themes/' . $this->theme . '/logins/'; $this->themeLevel1SetupNamespacedRoutes("logins"); } }
/** * 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); }
<?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();
<?php use Phalcon\Mvc\Router; $router = new Router(); $router->add('/', ['controller' => 'index', 'action' => 'index']); $router->add('/tokencheck', ['controller' => 'index', 'action' => 'tokencheck']); $router->handle();