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?

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
Could anyone help please?
mnakalay replied on at Permalink Reply
mnakalay
Check the core express form block. It uses redirection on submit
linuxoid replied on at Permalink Reply
linuxoid
Are you referring to this:
$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.
mnakalay replied on at Permalink Best Answer Reply
mnakalay
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
$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.
linuxoid replied on at Permalink Reply
linuxoid
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.
JohntheFish replied on at Permalink Reply
JohntheFish
You use URL::to to get the path to a page.
linuxoid replied on at Permalink Reply
linuxoid
I knew URL::to only returned a path.

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.