CakePHP, GET Parameters and routing

23,661

Solution 1

You can have your URL in any form. It's just CakePHP allows you to retrieve the variable passed through GET from the variable $this->params['url']

function myaction()
{
  if(isset($this->params['url']['arg'])) 
    $arg = $this->params['url']['arg'];
  if(isset($this->params['url']['arg2']))
    $arg2 = $this->params['url']['arg2'];
}

Solution 2

Solution in AppController for CakePHP 2.x

class AppController extends Controller {

....

/***
     * Recupera los Named envias por URL
     * si es null $key emtraga el array completo de named
     *
     * @param String $key
     *
     * @return mixed
     */
    protected function getNamed($key=null){
        // Is null..?
        if(is_string($key)==true){
            // get key in array
            return Hash::get($this->request->param('named'), $key);
        }else{
            // all key in array
            return $this->request->param('named');
        }
    }
...
}
Share:
23,661

Related videos on Youtube

Introgy
Author by

Introgy

Updated on March 02, 2020

Comments

  • Introgy
    Introgy about 4 years

    I am fairly new to cakephp but I have a question relating to urls and parameters. I would like to be able to have a url that looks like a standard url e.g:

    http://www.mysite.com/controller/myaction?arg=value&arg2=val
    

    I would like that url to map to an action in my controller as follows:

    function myaction($arg = null, $arg2 = null)
    {
       // do work
    }
    

    I realize that cakephp has routing as described here, however, honestly this seems over engineered and results in a url string that is nonstandard.

    In my current situation the url is being generated and invoked by an external (billing) system that knows nothing about cake and doesn't support the cake url format.

  • Jack Albright
    Jack Albright over 9 years
    You meant to say 'isset', right? is_set is not a php function.

Related