Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request alias is not identified in the container with RestfulStrategy #37

Closed
shadowhand opened this issue Mar 31, 2015 · 3 comments
Closed

Comments

@shadowhand
Copy link
Member

I am using league/container in my project and have this code to set up my container:

$container->add('Router', function () use ($container) {
    $router = new RouteCollection($container);
    $router->setStrategy(new RestfulStrategy);
    // ... list of routes
    return $router;
});

And then this code to dispatch the request:

$router     = $container->get('Router');
$request    = $container->get('Request');
$dispatcher = $router->getDispatcher();
$response   = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());

The problem is that when my controllers are called with action($request) the request is a new instance and not the request that was used to dispatch. I would suggest adding a method to RestfulStrategy that would allow the request name to be set:

public function setRequest($request) { ... }
public function getRequest() { ... }

public function dispatch($controller, array $vars)
{
    // ...
    $request = $this->getRequest() ?: $this->getContainer()->get(...);
    // ...
}

This would allow me to do the following, without having to use a FQCN in my container:

$container->set('DispatchStrategy', RestfulStrategy::class)
    ->withMethodCall('setRequest', ['Request']);
@shadowhand
Copy link
Member Author

Right now, to work around this, I have to do the following:

$container->add('Request', function() {
    return Request::createFromGlobals();
});

$container->add('Symfony\Component\HttpFoundation\Request', function () use ($container) {
    return $container->get('Request');
});

See also #26.

@philipobenito
Copy link
Member

Yes the aliasing will be getting added to the strategy for v1 shortly, as for v2, it will be using PSR-7 for the request and response objects.

The reason this was emitted originally was so that the user could have the choice of how to build the request object. If it is done for you then it's limiting any manipulation of the request.

@philipobenito
Copy link
Member

Closed as dupe of #26

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants