-
-
Notifications
You must be signed in to change notification settings - Fork 46
Remove UrlGenerator dependency from Redirect #245
Comments
You mean removing |
@samdark If
Ideally any middleware can be called anywhere in the middlewares chain. P.S. Yes, I know that it is not always possible to have such middlewares only, but in this specific case I think Another way would be just to have 2 redirect middleware. One simple redirect middleware without |
Agree. I'm fine with removing |
Ok, will send a PR soon. |
@yiiliveext suggested alternative approach:
This way it will be possible to use routes when defining redirects. |
Or toRoute() can accept UrlGeneratorInterface: Route::get('/superuser/profile')->addMiddleware((new Redirect($responseFactory))->toRoute('user/profile', [], $urlGenerator)) |
Or another alternative as suggested by @roxblnfk is to make Redirect a decorator around Response: class Redirect implements ResponseInterface
{
public function __construct(ResponseFactoryInterface $responseFactory)
{
}
// ...
} Then you can use it like this: public function actionX()
{
return (new Redirect($this->responseFactory))->toUrl('bla-bla');
} I like this last approach. |
From recent discussion. Usage: Route::get('/superuser/profile')
->addMiddleware(fn (Redirect $redirect) => $redirect->toRoute('user/profile')) Implementation: class Redirect
{
public function __construct(ResponseFactoryInterface $responseFactory)
{
}
public function toRoute(string $route, array $params): ResponseInterface
{
}
} |
@armpogart it actually works as is: Route::get('/superuser/profile', [...])
->addMiddleware(fn (Redirect $redirect) => $redirect->toRoute('user/profile')); |
The classic middleware approach (which I tried) would be Route::get('/redirect', [...])
->addMiddleware($container->get(Redirect::class)->toRoute('route')); or Route::get('/catch-all-route', $container->get(Redirect::class)->toRoute('route')); to call the middleware directly. Any correct middleware must be able to be used that way. @samdark As I understand the only way that could work is that in your case the actual middleware is the closure, and it is lazily invoked at later stage (when actually it is needed) with And if I think further it is more correct for the What do you think about implementing |
I'm fine with it. |
I propose to remove
UrlGenerator
dependency from Redirect middleware as it is impossible currently to use the middleware while defining the routes for the router as theUrlGenerator
can not be instantiated without having theRouter
.I have a use case when I want to redirect to other url on some simple condition/check in the router, I specify closure middleware, where I do my check and based on that check I chain
Redirect
middleware.My suggestion would be just to document the usage, where you can pass
UrlGenerator
generated router totoUrl
method.The text was updated successfully, but these errors were encountered: