thephpleague route - locale in uri

Hi I've switched to thephpleague route from a self written routingengine. My question is: Can i access wildcard variables outside of the route action method?

Example

Routing Part:

$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();

Controller part

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}
        // ];
    }
}

I could not find anything in the docs route.thephpleague

I'm using "league/route": "^1.2"

Answers


I think by default, you can only call methods in controller classes statically and when you do that the controller's constructor will not be called automatically. And also you can't use the route's wildcards to dynamically call controllers.

Please take note that this is not secure, but you should still be able to do what you want to happen with a Custom Strategy in league/route like this:


Controller

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;
    }
}

Custom Strategy

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);
    }
}

Routing with custom strategy integration

$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();

Need Your Help

How to really isolate stylesheets in the Google Chrome extension?

css google-chrome-extension isolation

I wrote a Google Chrome extension, which popups a dialog with an autocomplete field and it's own style, but there are some sites where my CSS gets totally broken, which doesn't look very nice.