Making CakePHP controller independent of how parameters are sent

Recently, I felt the need to make my CakePHP based controllers independent of how  parameters are sent. For instance, in Memento, I can handle parameters sent either in traditional way or using CakePHP style or even using the form (see usage below)
       Example (see how owner value is sent differently in the URL below)
           Traditional Way:  
           CakePHP way:  

 To make my controllers independent of how parameters are sent, I implemented this (below) small function in my app_controller class. This function takes care of how input (URL or form) parmeters are sent and returns the value, making your controllers independent of the logic to retrieve parameters.


  * retrieves a parameter value sent either in traditional way or using
  * cakephp way or using form
  * @param String/Array $parameters – (required)
  * @param String $dataKey – (optional) for retrieving data sent using form
  * returns Value/Array of values
  * Sample Usage:  getParameterValues(“id”)   , getParameterValues(array(“id”, “id2”)),  
  *                             getParameterValues(“id”, “User”), getParameterValues(array(“id”, “id2”), “User”)
 function getParameterValues($parameters, $dataKey = null){
  $isMultiple = true;

  //Check if single parameter or multiple parameters
      $parameters = array($parameters);
      $isMultiple = false;
  $values = array();

  //fetch parameters
  foreach($parameters as $key){
                  $values[$key] = $this->params[‘pass’][$key];
      else if(!empty($this->params[‘url’][$key]))
                 $values[$key] = $this->params[‘url’][$key];
     else if(!empty($dataKey) && isset($this->data[$dataKey][$key]))
                $values[$key] = $this->data[$dataKey][$key];
             return null;
  //if single parameter, then directly send the value. If multiple parameters, then send the value array
           return $values[$key];
          return $values;

Usage: (in your controller class)
1. If you want to retrieve single parameter
$owner = $this->getParameterValues(“owner”);  //here I want get “owner” parameter value

2. If you want to retrieve multiple parameters
$values = $this->getParameterValues(array(“owner”, “viewer”));
echo “owner = ” , $values[‘owner’] // contains owner value
echo “viewer = “, $values[‘viewer’] // contains viewer value

3. If values are sent using form
$data[‘User’][‘owner’]  = 2
$data[‘User’][‘viewer’] = 10
Then you can use the second optional parameter to get values from the form

$values  = $this->getParameterValues(array(“owner”, “viewer”), “User”);
echo “owner = ” , $values[‘owner’] // contains owner value
echo “viewer = “, $values[‘viewer’] // contains viewer value


About Ritesh Agrawal

I am a applied researcher who enjoys anything related to statistics, large data analysis, data mining, machine learning and data visualization.
This entry was posted in CakePHP and tagged . Bookmark the permalink.

One Response to Making CakePHP controller independent of how parameters are sent

  1. rafaelbandeira3 says:

    I think it’s a good practice to make your users follow rules, all and all everything has it’s rules, and so web apps. Allowing so many types of data pass will certainly lead to code inconsistence (no pattern followed thru the whole app), front end inconcistence (“funny this link is totally different…”), security holes (“why post if I can use query strings? … or named params…”).
    When writing the app base you should define wich conventions are you and your team going to follow, defining where query strings should be used and where named params should be caried out…
    I think with time you may end up dismissing this idea.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s