Welcome Guest, Not a member yet? Register   Sign In
Redirect from outside a controller
#5

(05-18-2020, 02:10 AM)imabot Wrote: Thank you for the advices, I'm already able to write these codes, but that's not what I call simple and concises when you have dozen of tests.

In my opinion, there is a fundamental problem with the redirection in CI4. Redirecting only from the controller is too restrictive.

I have not tested this but it should be worth a try.

In order to redirect from some non-controller class, you need to work directly with a Response object. Then you can use Response class methods redirect() and send() directly. You can get a Response object anywhere using Services.

PHP Code:
$response Services::response();
$response
    
->redirect($uri)
    ->send();
exit; 

You might find the RedirectResponse class to be handy instead of the vanilla Response class.  You have to call send() on it too.

That can also be done using the common function redirect() which returns a RedirectResponse  object.

PHP Code:
$response redirect($uri);
$response
    
->withInput()
    ->send();
exit; 
Reply


Messages In This Thread
Redirect from outside a controller - by imabot - 05-12-2020, 06:28 AM
RE: Redirect from outside a controller - by kick - 05-15-2020, 11:11 AM
RE: Redirect from outside a controller - by dave friend - 05-18-2020, 08:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB