Controller with index($id) method with parameter - 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: Controller with index($id) method with parameter (/showthread.php?tid=79964) |
Controller with index($id) method with parameter - Corsari - 08-25-2021 Is it possible to have a Controller with the index() method that accepts a parameter? e.g. controller called "Listjob" with method index($jobid) in such a way to allow this URL to work http://myapp.local/listjob/53 I have no custom routes set in app/Config/Routes.php and router setup is default PHP Code: $routes->setDefaultNamespace('App\Controllers'); The only way to make it work has been to abandon index($jobid) and just change its name from index to a different one , I used "show" but now as you may guess I have to browse to http://myapp.local/listjob/show/53 I also have tried to add a custom route PHP Code: $routes->add('listjob/(:any)', 'listjob/index/$1'); but again no success Thank you for nay hint regarding this RE: Controller with index($id) method with parameter - captain-sensible - 08-25-2021 i add a route for a "get request like" Code: $routes->get('(:any)', 'Pages::showme/$1'); where $something represents a changing end bit of url but on the right , it doesn't look as if your referencing any class or method you could directly call method index method on a class if its define but your route should be something like mine. Quickest way to play would probably be to edit home controller and add a method Try this Code: below : $routes->get('/', 'Home::index'); Find the controller Home.php in app/Controllers and edit add appropriately between class { and } : Code: public function test($something) then on your url http://yourapp/vegetables and see if "vegetables gets returned RE: Controller with index($id) method with parameter - plaztic - 08-25-2021 PHP Code: public function _remap() RE: Controller with index($id) method with parameter - Corsari - 08-25-2021 Thank you yes, it works thank to your hint , I went back to read the Routing section in the guide and the last two examples (like your) do the job https://codeigniter.com/user_guide/incoming/routing.html#examples and I've done like this PHP Code: $routes->add('jobs/(:num)', 'jobs::index/$1'); I was doing the route wrong instead of "::" I was using "/" RE: Controller with index($id) method with parameter - captain-sensible - 08-26-2021 glad its working :: is a scope resolution operator and / makes it a path or url |