本文整理汇总了PHP中Phalcon\Mvc\Router::add方法的典型用法代码示例。如果您正苦于以下问题:PHP Router::add方法的具体用法?PHP Router::add怎么用?PHP Router::add使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phalcon\Mvc\Router
的用法示例。
在下文中一共展示了Router::add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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());
}
示例2: 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;
}
示例3: testRoutes
/**
* @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');
}
示例4: addRoute
/**
* {@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());
}
示例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: 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();
$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);
}
示例8: 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));
}
示例9: setUp
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();
}
}
示例10: 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;
});
}
示例11: add
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);
}
}
示例12: createDependencies
/**
* @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;
}
示例13: addRoute
/**
* 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;
}
示例14: initRouter
/**
* 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;
};
}
示例15: Router
<?php
use Phalcon\Mvc\Router;
$router = new Router();
$router->add("/:controller/:action/:params", array('controller' => 1, 'action' => 2, 'params' => 3));
return $router;