• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do i redirect user to "home" controller after successfully loggin?

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")

  $data['error'] = 'Incorrect password/username entered';
  echo view('view_login'$data);
Exception $e){

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

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:

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

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

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?

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.

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

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

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

return redirect('home');

Thank you. God bless.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.