CodeIgniter Forums
Problem in CI v2 -> v4 project conversion (redirection / login / other controller) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Problem in CI v2 -> v4 project conversion (redirection / login / other controller) (/showthread.php?tid=77061)



Problem in CI v2 -> v4 project conversion (redirection / login / other controller) - juef - 07-16-2020

Greetings,

I have an old project I'm trying to convert to CI v4 from CI v2, and I'm running into a little problem. I had an Account controller which handled the site login. In the login function, I had something like this to load the site home (for which I have a controller and view) after login:

PHP Code:
redirect('/home'); 

Unfortunately, using redirect gives me a forInvalidRedirectRoute HTTP exception. I tried two other solutions:
  1. return redirect()->to('home'); Unfortunately, this seems to look for a home method in the Account controller class.
  2. echo view('home'); This loads the home.php view, but without a template and all the stuff it needs from its controller. The controller is made to load some news like so:

PHP Code:
$news_model = new News_model();

$data['news'] = $news_model->get_news();
$data['view'] = 'home.php';

echo 
view('template.php'$data); 

Obviously, I could simply copy the above code in the Account controller, but that wouldn't be elegant at all, and mostly, at some point I would love the login to redirect to the page the user was on. Obviously, it wouldn't make much sense to have basically every possible controller and view inside the login handler.

Any help or suggestion would be much appreciated. Thank you!


RE: Problem in CI v2 -> v4 project conversion (redirection / login / other controller) - InsiteFX - 07-17-2020

Try return redirect()->to('/home'); Not tested.


RE: Problem in CI v2 -> v4 project conversion (redirection / login / other controller) - juef - 07-21-2020

Thank you very much for your reply!

Unfortunately, it did the same as my first attempted workaround described above, which is very similar to your suggestion. I'll probably end up simply doing this:


PHP Code:
return redirect()->to(base_url());