![]() |
Auto Routing and redirect() not working - 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: Auto Routing and redirect() not working (/showthread.php?tid=85226) |
Auto Routing and redirect() not working - objecttothis - 12-06-2022 Converting an application from CI3.x to CI 4.1.3 My Controllers are in app/Controllers/ PHP Code: <?php My app/Config/Routes.php includes PHP Code: <?php but php spark routes includes no "auto" entries and I end up with an error when I navigate to https://[domain]/pos_dev/public CodeIgniter\HTTP\Exceptions\HTTPException route cannot be found while reverse-routing. RE: Auto Routing (Legacy) not working - InsiteFX - 12-06-2022 Please Read: CodeIgniter 4 User Guide - Controllers and Routing - Controllers - Auto Routing (Legacy) It is recommend that you not use Auto Routing (Legacy). You are using an old version of CodeIgniter 4 and should upgrade to the new version 4.2.10 and use the New Improved Auto Routing. RE: Auto Routing (Legacy) not working - objecttothis - 12-10-2022 It's been a crazy undertaking for me to convert this CI3.x app to 4.1.3, so I was trying to get through that before upgrading then to 4.2.10. Improved Auto Routing however convinced me, so I updated CI4 to 4.2.10 and enabled Improved Auto Routing per the user guide (https://codeigniter.com/user_guide/incoming/routing.html#auto-routing-improved). app/Config/Routes.php PHP Code: $routes->setAutoRoute(true); app/Config/Feature.php PHP Code: public bool $autoRoutesImproved = true; app/Controllers/Home.php is setup PHP Code: <?php So I don't understand why even the improved auto routing is not picking up the controller. when I navigate to the public folder I get a slightly different error now: CodeIgniter\HTTP\Exceptions\HTTPException The route for "home" cannot be found. SYSTEMPATH/HTTP/RedirectResponse.php at line 60 The end of the index() function in my Login controller has PHP Code: return redirect('home'); PHP Code: $routes->get('/', 'Login::index'); RE: Auto Routing (Legacy) not working - kenjis - 12-10-2022 Auto Routing (Legacy) should work with your first config. I don't know why it does not work. Auto Routing (Improved) is much complex than Legacy for the security reasons. Read this carefully: https://codeigniter4.github.io/CodeIgniter4/changelogs/v4.2.0.html#new-improved-auto-routing Redirect() not working - objecttothis - 01-30-2023 (12-10-2022, 05:58 PM)kenjis Wrote: Auto Routing (Legacy) should work with your first config. See below. The issue is now redirect(). After returning from vacation I upgraded the core framework to 4.3.1 then walked through all the upgrade path steps. Still no dice, so to troubleshoot I commented out all my defined routes except for the root directory $routes->get('/', 'Login::getIndex');. Then I ran php spark routes again and the autorouting began working. So, I figured that it was one of my defined routes breaking auto routing of all controllers. I uncommented each defined route individually down to uncommenting all of them but autorouting remained working. Oh well. After all that I'm still getting "The route for "testRoute" cannot be found." I can't seem to attach images, so I'll have to write this all in text. In Routes.php I have: PHP Code: $routes->get('/', 'Login::getIndex'); That is routing properly. At the end of the getIndex function in the Login controller I have: PHP Code: return redirect('testRoute'); Under app\Controllers\ I have TestRoute.php PHP Code: <?php When I navigate the browser to /public/ it loads getIndex() from Login.php but when it gets to the last line it kicks out: PHP Code: The route for "testRoute" cannot be found. It doesn't seem to matter what I change the controller to in the redirect() function. php spark routes clearly shows: PHP Code: | GET(auto) | testRoute | | \App\Controllers\TestRoute::getIndex | honeypot csrf invalidchars | honeypot secureheaders toolbar | It's unclear to me why redirect isn't working. Can redirect() no longer be used with auto routing like it could be in CI3? If I navigate to /public/testRoute/ it displays "hello world" as it should. RE: Auto Routing and redirect() not working - kenjis - 01-30-2023 redirect() is completely different from redirect() in CI3. See https://codeigniter4.github.io/CodeIgniter4/general/common_functions.html#redirect If you write redirect('testRoute'), the testRoute is a route name, not URI path. A route name is a name for a defined route. Try: PHP Code: return redirect()->to('testRoute'); RE: Auto Routing and redirect() not working - objecttothis - 01-30-2023 OK, PHP Code: return redirect()->to('testRoute'); PHP Code: redirect('home'); PHP Code: return redirect()->to('home'); Thanks very much for your help here! |