Route with variable gives 404 - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: Route with variable gives 404 (/showthread.php?tid=78189) |
Route with variable gives 404 - sjender - 12-11-2020 Hi all, I'm new to CodeIgniter and have a routing issue. What I try to do is add a variable to the URL to pass it into the method arguments. I used this example from the CI manual: PHP Code: $routes->add('product/(:num)', 'Catalog::productLookupByID/$1'); The actual code to match my needs is: PHP Code: $routes->get('/backend/profile/', 'backend/Profile::index'); The method in the controller is: PHP Code: public function index($var = null): void When I run the URL http://localhost:8080/backend/profile/1 I expect it to echo "1". But instead a 404 is thrown [Controller or its method is not found: \App\Controllers\backend::index] This controller does exist, because when I remove the 1 from the URL, the page loads, however, it is echoing NULL obviously... What is this beginner missing here??? RE: Route with variable gives 404 - neoneeco - 12-12-2020 You have to capitalize the backend, and check your slashes RE: Route with variable gives 404 - sjender - 12-14-2020 I now have copied the exact syntax including slashes, and capitailized backend as you said, but sadly it doesn't work PHP Code: $routes->get('backend/brands/(:any)', 'Backend/Brands::search/$1'); When I remove the /$1 at the end, the routing comes to the right page, but without the variable... RE: Route with variable gives 404 - ReivaxBird - 12-14-2020 Hello, could you try : PHP Code: $routes->add('backend/brands/(:any)', 'Backend\Brands::search/$1'); In the documentation you have this example : PHP Code: $routes->add('product/(:num)', 'App\Catalog::productLookup'); it's not a / but a \ I think and they use add instead of get. If I have to use get and post i use match instead of add. with as first parameter ['get','post']. RE: Route with variable gives 404 - paulbalandan - 12-17-2020 PHP Code: $routes->get('/backend/profile', 'Backend\Profile::index'); RE: Route with variable gives 404 - John_Betong - 12-17-2020 @sjender, Try the most complicated route first followed by a simpler route: // instead of this $routes->get('/backend/profile/', 'backend/Profile::index'); $routes->get('/backend/profile/(:num)', 'backend/Profile::index/$1'); // try this $routes->get('/backend/profile/(:num)', 'backend/Profile::index/$1'); $routes->get('/backend/profile/', 'backend/Profile::index'); RE: Route with variable gives 404 - includebeer - 12-21-2020 (12-17-2020, 08:57 AM)paulbalandan Wrote: Paulbalandan is right. The second parameter must match your namespace and use backslashes. |