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

2012-06-28 Thread Jeremy Burns | Class Outfit
What happens if you pass an $id that is an integer but that $id doesn't exist 
in the books table?

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 28 Jun 2012, at 11:26:40, Poyan Nabati wrote:

 Lets say I have a controller Books with an action view() defined as follows: 
 
 class BooksController extends AppController {
 public function view($id) {
 // something
 }
 }
 
 Let's assume that $id is supposed to be of a numeric type. 
 
 I see two options; 
 
 1) Setup the route so that it only connects for instance books/view/{integer} 
 to  Books-view($id)
 2) Have general routing like the defaults in CakePHP and include code in 
 view() to make sure that $id is set and is numeric. 
 
 Which is better practice? 
 
 In my view I think the former is cleaner and less magicky. This is something 
 I enjoyed working with the Play Framwork, that you explicitly had to connect 
 a route to each action. 
 
 What is your opinion?
 
 -- 
 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+unsubscr...@googlegroups.com For more options, visit this group at 
 http://groups.google.com/group/cake-php

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


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

2012-06-28 Thread Poyan Nabati
That's another type of error though. It's something have to handle in your 
application but I don't see the relation to my OP. 

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


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

2012-06-28 Thread Jeremy Burns | Class Outfit
I disagree. I think you're not using routes in their purest form (to route 
stuff in the right way - rather than error trapping) and even if it passes you 
still need to check the validity in the controller and redirect if necessary. 
Why do that in two places? And what happens in the router if a non-integer is 
passed?

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 28 Jun 2012, at 12:58:36, Poyan Nabati wrote:

 That's another type of error though. It's something have to handle in your 
 application but I don't see the relation to my OP. 
 
 -- 
 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+unsubscr...@googlegroups.com For more options, visit this group at 
 http://groups.google.com/group/cake-php

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


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

2012-06-28 Thread Dr. Tarique Sani
Apart from what Jeremy said you should also remember that routes are
expensive. Use them wisely

Tarique

On Thu, Jun 28, 2012 at 5:36 PM, Jeremy Burns | Class Outfit
jeremybu...@classoutfit.com wrote:
 I disagree. I think you're not using routes in their purest form (to route

snip

-- 
=
PHP for E-Biz: http://sanisoft.com
=

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


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

2012-06-28 Thread Poyan Nabati
I think you're not using routes in their purest form (to route stuff in 
the right way - rather than error trapping. 

Thing is, if you haven't supplied an integer, then there's really no 
matching action for the URL you've requested. I think that's completely in 
line with the purpose of routes. 

That is, if you supply a non-integer, then you haven't really matched any 
existing action, so you're redirected to an error or a default page.

Since PHP doesn't really have strict typing, maybe I could settle for if 
you haven't passed the right number of parameters, there's no matching 
action.

Why do that in two places?

I think it could result in cleaner code since you can be sure of the type 
and amount of input you receive in each action.

Don't you agree?

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


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

2012-06-28 Thread Kamisama
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php