Route with variable gives 404 |
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???
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...
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'].
PHP Code: $routes->get('/backend/profile', 'Backend\Profile::index');
@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');
|
Welcome Guest, Not a member yet? Register Sign In |