When passing get params into a function in a framework.

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

When passing get params into a function in a framework.

Post by panic! »

Yo,

I'm just thinking about passing GET variables into functions in frameworks.

When passing get params into a an action of a controller in a framework would you prefer...

The cake way:

http://www.site.com/dog/feed/meaty-chunks/100
then goes to:
<?

class dog_controller
{
function feed($food,$amount)
{
echo "I just ate $amount $food";
}

}

?>
or the variables named in the url like this..


http://www.site.com/dog/feed/food/meaty ... amount/100
<?
class dog_controller
{
function feed()
{
echo "I just ate ".$request->get->amount." ". $request->get->food;
}

}
?>

Which way do you prefer and why? Or maybe there's another commonly used method I don't know of that I should be looking at?

I'd be very grateful for any input : )
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: When passing get params into a function in a framework.

Post by panic! »

Whoops could someone please move this into theory and practice :oops:
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: When passing get params into a function in a framework.

Post by alex.barylski »

I prefer a request method so I have explicit control over the sanitization of the incoming data. I'm not sure if CakePHP does any checks on data before passing into a controller but it would be easier to forget to check data if they were passed as arguments. Not only that but if the parameters change, wouldn't you need to update the controller API?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: When passing get params into a function in a framework.

Post by josh »

A request object for getting request parameters, a command, or config object or regular scalar parameter ( ultimately derived from a request object ) for invoking models. Are you trying to write a framework or use one though?
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: When passing get params into a function in a framework.

Post by panic! »

Neither really, I'm just curious to hear the arguments for either way.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: When passing get params into a function in a framework.

Post by crazycoders »

Sanitization should occur at the REQUEST object level and then the values should be passed to the controler that validate the content of it. Just make sure that the sanitization doesn't prevent access to features such as sending HTML. ASP.NET has/had this problem and it's creating a security whole instead of fixing one, cause people have to turn off sanitization.

Secondly, the controller should access the request object himself... He IS the controller after all, you as the user of the framework should not provide the information to the controler or you become a controler of the controler. The C is there to actually control the flow process so he should know where to get it's data from.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: When passing get params into a function in a framework.

Post by josh »

If you need to vary control based on a parameter like that I'd make an abstract command class, sub class that and use it from within the controller as a strategy pattern, so that your design doesn't depend on parametrized control flows, either that or just look at the request object and conditionally forward to another module/controller/action
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: When passing get params into a function in a framework.

Post by josh »

Take a look at how Zend does it.

Code: Select all

 
// forward to an action in another controller:
    // FooController::bazAction(),
    // in the current module:
    $this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
 
when you choose to dispatch additional actions, you can define an optional 4th parameter which is an array of request parameters to send to the next action, that way no parameters ever get hard coded for the framework implementor
Post Reply