Thursday, June 28, 2012

Re: Routes vs Type checking for Action Parameters, best practice?

"Why do that in two places?"

Because it does not do the same thing.

You always have to validate the $id in the controller, since as already said, the $id can be a perfect integer, but does not exist in the database.

I assume your example in the router file will be like that :

Router::connect('/books/view/:id', array('controller'=>'books', 'action'=>'view'), array('pass' => array('id'), 'id' => '[0-9]+?'));

There's no benefits of enforcing a numeric validation on the ID in this case. But if you have routes like that :

Router::connect('/books/:id', array('controller'=>'books', 'action'=>'view'), array('pass' => array('id'), 'id' => '[0-9]+?'));
Router::connect('/books/:action', array('controller'=>'books', 'action'=>'add'));

making sure the id is numeric is necessary, else the second route will never be read, and all your url like "books/add", or whatever will be redirect to the view action.

Type validation in the routes are to redirect the flow to the right action in the right controller, for the routes that can be ambiguous.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: