端くれプログラマの備忘録 CakePHP [CakePHP] リクエストの取得方法

[CakePHP] リクエストの取得方法

GETのクエリ文字列

/posts/index?page=1&sort=title
function index() {
    $page = $this->request->query('page'); //'1'
    $sort = $this->request->query('sort'); //'title'
}

名前無しパラメータ

/posts/index/10/20
function index($param1, $param2) {
    $param1; //10
    $param2; //20
}

あるいは

function index() {
    $this->params['pass'][0]; //10
    $this->params['pass'][1]; //20
}

名前付きパラメータ

/posts/index/param1:10/param2:20
function index() {
    $this->params['named']['param1']; //10
    $this->params['named']['param2']; //20
}

名前無し&名前付きパラメータ混在

/posts/index/param1:10/30/param2:20/40/50
function index() {
    $this->params['named']['param1']; //10
    $this->params['named']['param2']; //20

    $this->params['pass'][0]; //30
    $this->params['pass'][1]; //40
    $this->params['pass'][2]; //50
}

参考サイト

【CakePHP】リクエストの取得方法 – Qiita
http://qiita.com/kazu56/items/7d344ccef56deef66a7a

名前付きでパラメータを受け取る方法 – Writing Some Code
http://d.hatena.ne.jp/ngtn/20080403/1207231914