10

After logout, I tried to redirect for the home page. I tried to few ways, but not redirected.

class User extends BaseController
{
    public function __construct()
    {
        helper('url');
    }

for the logout function. I used three ways

redirect('/');

or

header("Location:".base_url());

or

route_to('/');
Senthil
  • 757
  • 2
  • 7
  • 25

9 Answers9

32

as per CI 4

use

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

if you are using route then use

return redirect()->route('named_route');
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37
  • you are right but how we can redirect to particular controller like we can do in CI 3 redirect('conroller/method') ? I don't think we should do it like return redirect()->to(base_url().'/public/conroller/method','refresh'); ? – Ajit Bhandari Mar 14 '20 at 13:05
  • @Samiul plz can you show the code, why not working? – Devsi Odedra Aug 08 '20 at 05:22
  • Anyone who wants to redirect a route forcefully, You can do it by `redirect('','location',304)->send();` Redirect function creates `RedirectResponse ` object which is consumed by CI runtime and the class is extended upto MessageInterface. Because only controller methods are excepted to create Messages i.e. Response and Request objects, it defaults to creating proper response object which is built and then proper send method is called. Hope this helps anyone who is stuck with documentation. – Maulik Parmar Feb 23 '21 at 08:45
4

I use this and it works

return redirect()->to(site_url());
2

In codeigniter 4 redirect()->to() returns a RedirectResponse object, which you need to return from your controller to do the redirect.

for ex.

class Home extends BaseController {
    public function index() {
        return redirect()->to('https://example.com');
    }
}
Inc33
  • 1,747
  • 1
  • 20
  • 26
2

Its worth saying that unlike the former CI3 redirect() function this one must be called from within a Controller. It won't work for example within a Library.

Update 2021

It is in fact possible to do this! Simply check that the returned response is an object and return it instead. So if a library returns a RedirectResponse, check it using the following code and return if applicable.

if (!empty($log) && is_object($log)){
    return $log;
}

You could of course do get_class() to make sure the object is a type of RedirectResponse if there is any possibility of another object being returned.

Antony
  • 3,875
  • 30
  • 32
0

I am new to CI4. In my case, I had to properly set $baseURL in App.php. For example, if the port is set incorrectly in your local development, it will just hang.

eg. public $baseURL = 'http://localhost:8888/';

user1889992
  • 355
  • 3
  • 7
0

If you using unnamed route:

$this->response->redirect(site_url('/user'));

'/user': It is my controller name. You can also used controller/function name.

Elikill58
  • 4,050
  • 24
  • 23
  • 45
RISHABH JAIN
  • 11
  • 1
  • 2
0

Please look at the documentation

// Go back to the previous page return redirect()->back();

// Go to specific URI return redirect()->to('/admin');

// Go to a named route return redirect()->route('named_route');

// Keep the old input values upon redirect so they can be used by the old() function return redirect()->back()->withInput();

// Set a flash message return redirect()->back()->with('foo', 'message');

// Copies all cookies from global response instance return redirect()->back()->withCookies();

// Copies all headers from the global response instance return redirect()->back()->withHeaders();

  • This is an answer to a already answered question from a couple of years ago. It's badly formatted and the first line is: "Please look at the documentation" It doesn't really help the op. – Kerel Mar 14 '22 at 10:01
-1

If you find:

{0, string} route cannot be found while reverse-routing

This error:

Please Go to system\HTTP\RedirectResponse Line no 91 :

Change:

throw HTTPException::forInvalidRedirectRoute($route);

To:

return $this->redirect(site_url('/Home'));  
(dashboard after login)
Hoppo
  • 1,130
  • 1
  • 13
  • 32
-5

The redirect statement in code igniter sends the user to the specified web page using a redirect header statement. This statement resides in the URL helper which is loaded in the following way:

 $this->load->helper('url');

The redirect function loads a local URI specified in the first parameter of the function call and built using the options specified in your config file.

The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".

According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem."

Example:

if ($user_logged_in === FALSE)
{
 redirect('/account/login', 'refresh');
}

Original Answer: https://stackoverflow.com/a/725200/5700401

Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43