【发布时间】:2015-12-03 15:46:34
【问题描述】:
您好,我已经从一个自写的路由引擎切换到 phpleague 路由。我的问题是:我可以在路由操作方法之外访问通配符变量吗?
示例
路由部分:
$router = new League\Route\RouteCollection;
$router->addRoute('GET', '{locale}/{controller}/{action}', '\Backend\Controller\{controller}Controller::{action}');
$dispatcher = $router->getDispatcher();
//making a call with, for example, '/en/foo/bar', or '/de/foo/bar'
$response = $dispatcher->dispatch($oRequest->getMethod(), $oRequest->getPathInfo());
$response->send();
控制器部分
class FooController extends AppController {
public function __construct() {
//<---- here i want to access the {locale} from the URI somehow
}
public function bar(Request $request, Response $response, array $args) {
// $args = [
// 'locale' => 'de', // the actual value of {locale}
// 'controller' => 'foo' // the actual value of {controller}
// 'action' => 'bar' // the actual value of {bar}
// ];
}
}
我在文档route.thephpleague 中找不到任何内容
我正在使用“联赛/路线”:“^1.2”
【问题讨论】:
-
我认为在构造函数中无法访问它,但您可以尝试自己解析 $_SERVER['REQUEST_URI']
标签: php routes request uri locale