Reputation: 980
I have a controller in my CakePHP app called 'Profiles', and this is what my action called index is:
function index($id = NULL) {
$this->set('profile', $this->Profile->findById($id));
}
So using this method to pass in the id, I can then return the correct profile depending on the id that is in the link.
This works fine when I do this on an action called view, however when I try to do it on the index action it treats my id as if it were an action, and returns an error saying that the action doesn't exist. Is there a way to pass in the id on the index action in CakePHP, or does it have to be on an action besides the index such as a view action?
Cheers,
Adam.
Upvotes: 1
Views: 252
Reputation: 3815
This can be done with a custom route. Pass a parameter to the action...
Router::connect('/mycontroller/:id', array('controller' => 'mycontroller', 'action' => 'index'), array('pass' => array('id') ) );
and in your controller...
public function index( $id )
http://book.cakephp.org/3.0/en/development/routing.html#passing-parameters-to-action
Upvotes: 0
Reputation: 7585
If you want to pass an id to your method you have to visit a url with an id in it.
/profiles/index/<id_here>
Cake will load /profiles
as /profiles/index
magically. You can not use /profiles/<id>
out the box, that would require a new Route before it would work.
Upvotes: 1
Reputation: 1756
If you used the Bake function to create your controller, the Index method would be used to display several records from the Profile model.
Is the three lines of code you displayed in your question the actual code from your controller?
Upvotes: 0