Controller with index($id) method with parameter |
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
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
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/incom...l#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 "/"
|
Welcome Guest, Not a member yet? Register Sign In |