CodeIgniter Forums
How do i redirect user to "home" controller after successfully loggin? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: How do i redirect user to "home" controller after successfully loggin? (/thread-72681.html)



How do i redirect user to "home" controller after successfully loggin? - tebogosebake - 01-23-2019

PHP Code:
I have this method for login
public function authenticate(){
//get formm input into an array
$login_data = array(
'username' =>$this->request->getPost("username"),
'password' =>$this->request->getPost("password")
);

try{
  if($this->model_login->login($login_data)){
//redirect(base_url('home')); 
  redirect('home'); 
}else{
  $data['error'] = 'Incorrect password/username entered';
  echo view('view_login'$data);
}
}catch(\
Exception $e){
die(
$e->getMessage());
}


When the user succesfully login it throws this message "{0, string} route cannot be found while reverse-routing" instead of redirecting to "Home: controller. 


RE: How do i redirect user to "home" controller after successfully loggin? - kilishan - 01-23-2019

A couple of things here.

First - you should return the redirect() result as a best practice.

Second - as a security thing when using redirect like you are here, it uses reverse routing. If you don't want to do it that way you can do it like so:

Code:
return redirect()->to('home');



RE: How do i redirect user to "home" controller after successfully loggin? - tebogosebake - 01-23-2019

Thank you! The return redirect()->to('home'); option worked like a charm but the reverse routing is showing me a ghost maybe its because i am still new and getting the hang of codeignter 4.

I added this following in my Route.php
$routes->add('home','Home::index');

Then in my login controller i returned it like:
return redirect(base_url('home'));

but i am still getting this:
{0, string} route cannot be found while reverse-routing.

What am i missing?


RE: How do i redirect user to "home" controller after successfully loggin? - kilishan - 01-24-2019

Per the userguide:

Quote:Reverse routing allows you to define the controller and method, as well as any parameters, that a link should go to, and have the router lookup the current route to it.

So in your redirect call you have to give it the controller and method to go to, not the URI.

Code:
return redirect('App\Controllers\Home::index');

Alternatively, if you had "named" your route, you could provide the route's name instead.

Code:
$routes->get('home', 'Home::index', ['as' => 'home']);

return redirect('home');



RE: How do i redirect user to "home" controller after successfully loggin? - tebogosebake - 01-25-2019

Thank you. God bless.