Welcome Guest, Not a member yet? Register   Sign In
How do i redirect user to "home" controller after successfully loggin?
#1

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. 
Reply
#2

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');
Reply
#3

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?
Reply
#4

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');
Reply
#5

Thank you. God bless.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB