本文整理汇总了PHP中Phalcon\Mvc\Router类的典型用法代码示例。如果您正苦于以下问题:PHP Router类的具体用法?PHP Router怎么用?PHP Router使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Router类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: registerServices
/**
* 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);
}
示例2: setDi
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;
}
示例3: modulesClosure
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());
}
示例4: initRouter
protected function initRouter()
{
$this->getDI()->set('router', function () {
$router = new Router();
$router->setDefaultModule('hrm');
return $router;
});
}
示例5: mvcRouter
private function mvcRouter()
{
//Register routing
$router = new Router();
$router->clear();
foreach ($this->config('route') as $url => $route) {
$router->add($url, $route->toArray());
}
return $router;
}
示例6: createFrom
/**
* @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;
}
示例7: addRoutes
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));
}
示例8: registerServices
/**
* 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);
}
示例9: zcms_load_frontend_router
/**
* 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;
}
示例10: __construct
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']);
}
示例11: collectParams
/**
* @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;
}
示例12: setUp
/**
* 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;
});
}
示例13: handle
/**
* 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;
}
示例14: setUpDispatcher
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);
}
示例15: handle
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;
}