'(CI 4) How to redirect within Controller?

how can I refer to another within one controller? In CI3 (for example in the controller client.php) I solved this as follows:

redirect('/Clients', 'refresh');

but that no longer seems to work in CI4. (Msg: "route cannot be found while reverse-routing.") I Also tried

redirect()->route('/Clients');

but the error is the same.

redirect()->to('/Clients');

redirects nowhere (no output, nothing)

For a better understanding: I want to use a controller (e.g. Clients/create to Clients/details)



Solution 1:[1]

What you should notice is that redirect() does not just set headers like it used to do in CI3. In CI4 it returns a RedirectResponse object with which you can ask your controller to do a redirection.
To do so, you need to return this RedirectResponse object inside your controller. Without the return statement, the redirection won't happen.

An other thing to notice is that redirect() can be called with some "options" :

Going to a named route

return redirect()->route('named_route'); 

or

 return redirect('named_route');

To use this, you need to add a named routes in your app/Config/Routes.php file :

$routes->get('/', 'MyController::index', ['as' => 'named_route']);

Going to a specific URI

return redirect()->to('Clients');

It will redirect you to your base url with /Clients at the end.

Please check out the doc for further informations : https://codeigniter.com/user_guide/general/common_functions.html#redirect

Solution 2:[2]

Codeingiter 4 Docs are not super clear on this. They don't give you all the different options available for redirects. I won't begin to list them all here but to find more options have a look in the system->HTTP directory most the files there have function that can be used with the redirect() function, mainly the ResponseTrait.php file.

For example to do a redirect and if you want to send the headers immediately you can do this:

redirect('my_route')->sendHeaders();
die();

If for example you have a controller that extends another and is accessed by the parent controller from the browser and try to use the following:

 return redirect('named_route');

It may not work because the route is an object that is returned to the parent controller. If in the parent controller you return it then it will work. (i.e.)

class Home extends BaseController {

   public function my_handler($funcCall=''){
     
     // call function in child controller
     $response = $this->$funcCall();

     // Handle Response from child controller...

   }
}

class Childcontroller extends Home {

    protected function someData(){
        // do something here...

        if($this->request->isAJAX()){
          // Do something...
          
        }else{

          // have to return again in parent controller
          // or won't redirect
          return redirect('some_route');

          // Else you can use 
          redirect('my_route')->sendHeaders();
          die();
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ViLar
Solution 2 Kyle Coots