ContollerやModelでリンク、URLの生成の仕方

ページURLの生成方法

Viewでは、$this->Html->url('/controller/action/', true);のようにHelperを利用するが、ContollerやModelでは、下記のようにする。

ページURL(httpから)の生成方法

$url = Router::url('/controller/action/', true);
$url = Router::url(array('controller' => 'members', 'action' => 'add', $serial),true);

// CakephpでのルートへのURLを出力したい場合
$url = Router::url('/', true);

URLで渡されたパラメタ取得方法

http://sample.com?value=100 の場合

$this->params['url']['value']; //100

redirectでパラメタを渡す方法

$this->redirect(array('action' => 'index', '引数1', '引数2'));
// -> xxx/index/引数1/引数2

$this->redirect(array('action' => 'index', 'id'=>'引数1', 'name'=>'引数2'));
 // -> xxx/index/id:引数1/name:引数2

リファラーURLへリダイレクト

$this->redirect($this->referer());

タイトルとURLをコピーしました