【问题标题】:thephpleague route - locale in urithephpleague 路由 - uri 中的语言环境
【发布时间】: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


【解决方案1】:

我认为默认情况下,您只能静态调用控制器类中的方法,并且当您这样做时,控制器的构造函数不会被自动调用。而且你不能使用路由的通配符来动态调用控制器。

请注意,这并不安全,但您仍然可以使用Custom Strategy 在联赛/路线中做您想做的事情,如下所示:


控制器

class TestController  {

    public function __construct($args) {
        //the wildcards will be passed as an array in the constructor like this
        $this->locale = $args['locale'];
    }

    public function check(Request $request, Response $response, array $args) {
        // $args = [
        //     'locale'   => 'de',  // the actual value of {locale}
        //     'controller' => 'Test' // the actual value of {controller}
        //     'action' => 'check' // the actual value of {action}
        // ];

        return $response;
    }
}

自定义策略

class CustomStrategy implements StrategyInterface {
    public function dispatch($controller, array $vars)
    {
        $controller_parts = [];
        foreach($controller as $con){
            foreach ($vars as $key => $value) {
                $placeholder = sprintf('{%s}', $key);
                $con = str_replace($placeholder, $value, $con);
            }
            $controller_parts[] = $con;
        }

        //the controller will be instantiated inside the strategy
        $controllerObject = new $controller_parts[0]($vars);

        //and the action will be called here
        return $controllerObject->$controller_parts[1](Request::createFromGlobals(),new Response(),$vars);
    }
}

带有自定义策略集成的路由

$router = new League\Route\RouteCollection;
$router->setStrategy(new CustomStrategy()); //integrate the custom strategy
$router->addRoute('GET', '/{locale}/{controller}/{action}', '{controller}Controller::{action}');

$dispatcher = $router->getDispatcher();

//if your url is /en/Test/check, then TestController->check() will be called
$response = $dispatcher->dispatch($oRequest->getMethod(), $oRequest->getPathInfo());

$response->send();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 2013-11-17
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多