8.4.2 What's the alternative to redirect?
Permalink
As the controller's redirect() is deprecated, what's the alternative?
Someone suggested to use URL::to() instead, but it simply returns the URL, what's the actual redirect alternative? How to use the URL::to to redirect?
Someone suggested to use URL::to() instead, but it simply returns the URL, what's the actual redirect alternative? How to use the URL::to to redirect?
Could anyone help please?
Check the core express form block. It uses redirection on submit
Are you referring to this:
I don't see how this is a substitute for the redirect because I don't return anything, I simply need to go to a page, not necessarily within an action.
$processor = $controller->getFormProcessor(); $r = Redirect::to($url); $r->setTargetUrl($r->getTargetUrl() . '#form' . $this->bID); return $processor->deliverResponse($entry, ProcessorInterface::REQUEST_TYPE_ADD, $r);
I don't see how this is a substitute for the redirect because I don't return anything, I simply need to go to a page, not necessarily within an action.
the fact that you don't see it doesn't mean it's not there and your question said "controller" which is highly non-specific.
Anyway, if you look deeper you will see that doing Redirect::to() creates a redirect response with a 302 code (redirected but not permanent)
Redirect::page() allows you to specify your own redirect code instead of 302.
That object you get is an instance of Symfony\Component\HttpFoundation\RedirectResponse which extends Symfony\Component\HttpFoundation\Response.
That, in turn, can be triggered by the send() function.
so you can do
What's more, if you look at the deprecated Redirect() function you will see
Which basically does exactly what I told you. It takes your arguments and use the proper Response instance instead of the old deprecated code.
All of this is slightly below the surface in the core code, you just have to look.
Anyway, if you look deeper you will see that doing Redirect::to() creates a redirect response with a 302 code (redirected but not permanent)
Redirect::page() allows you to specify your own redirect code instead of 302.
That object you get is an instance of Symfony\Component\HttpFoundation\RedirectResponse which extends Symfony\Component\HttpFoundation\Response.
That, in turn, can be triggered by the send() function.
so you can do
$r = Redirect::to('/your/path'); $r->send(); exit;
What's more, if you look at the deprecated Redirect() function you will see
public function redirect() { $args = func_get_args(); $r = call_user_func_array(['Redirect', 'to'], $args); $r->send(); exit; }
Which basically does exactly what I told you. It takes your arguments and use the proper Response instance instead of the old deprecated code.
All of this is slightly below the surface in the core code, you just have to look.
A PRB reviewer made a comment for my package: "redirect is deprecated, use URL::to instead". Nothing was said about Redirect::to. That's why I was insisting on an example how to use URL::to.
Thank you for your help.
Thank you for your help.
You use URL::to to get the path to a page.
I knew URL::to only returned a path.
The comment was to replace
for URL::to. I could not find that anywhere in the documentation. I guess he made a mistake, it should have been Redirect::to.
The comment was to replace
$this->redirect('/dashboard/simple_comments/settings/success');
for URL::to. I could not find that anywhere in the documentation. I guess he made a mistake, it should have been Redirect::to.