PHP

CakePHPのルーティング例

投稿日:2014年9月26日 更新日:

Webサイトではアドレスの構造は重要です。
CakePHPのルーティングの例について紹介します。

1. Router::connect(‘/index/’, array(‘controller’ => ‘test’, ‘action’ => ‘index’));
「http://xxxxxxxxxxxxx/index/」で、
testコントローラのindex()が呼ばれます
class TestController extends AppController {
function index() {
}
}

2. Router::connect(‘/index2/’, array(‘controller’ => ‘test’, ‘action’ => ‘index2’, 1));
「http://xxxxxxxxxxxxx/index2/」で、
testコントローラのindex2()が呼ばれ、引数に「1」が渡ります。
class TestController extends AppController {
function index2($arg) {
debug($arg); // (int) 1と表示されます
}
}
3. Router::connect(‘/index3/’, array(‘controller’ => ‘test’, ‘action’ => ‘index3’, array(“category” => “テスト”)));
「http://xxxxxxxxxxxxx/index3/」で、
testコントローラのindex3()が呼ばれ、引数に配列「array(“category” => “テスト”)」が渡ります。
class TestController extends AppController {
function index3($args) {
debug($args); // array(‘category’ => ‘テスト’)と表示されます
}
}

4. Router::connect(‘/index4/’, array(‘controller’ => ‘test’, ‘action’ => “index4”, “category” => “テスト2”));
「http://xxxxxxxxxxxxx/index4/」で、
testコントローラのindex4()が呼ばれ、$this->paramsに「array(“category” => “テスト2”)」が設定されます。
class TestController extends AppController {
function index4() {
debug ($this->params[“category”]); // ‘テスト2’と表示されます。
}
}

5. Router::connect(‘/index5/’, array(‘controller’ => ‘test’, ‘action’ => ‘index5’, “prefix” => “admin”));
「http://xxxxxxxxxxxxx/index5/」で、
testコントローラのadmin_index5()が呼ばれます
class TestController extends AppController {
function admin_index5() {
}
}

6. Router::connect(‘/index6/*’, array(‘controller’ => ‘test’, ‘action’ => “index6”));
「http://xxxxxxxxxxxxx/index6/テスト/テスト2/テスト3」で、
testコントローラのindex6()が呼ばれ、「テスト」が$arg1, 「テスト2」が$arg2, 「テスト3」が$arg3に設定されます
class TestController extends AppController {
function index6($arg1, $arg2, $arg3) {
debug ($arg1); // ‘テスト’と表示されます
debug ($arg2); // ‘テスト2’と表示されます
debug ($arg3); // ‘テスト3’と表示されます
}
}

-PHP

関連記事

Movable Type6がリリースされました

Movable Type6がリリースされました

movable type6 以下の新機能が追加されたとのことです。 – Data API APIを通じてMovable Typeの管理画面の操作や記事の読み込みが可能になったとのことです。 – Cha …

CakePHPのモデルの便利な機能について

CakePHPのモデルの便利な機能について

CakePHPのモデルにあります便利な機能について紹介します。 – virtualFields ブログのエントリーのテーブルがありまして、 カテゴリごとのエントリー数を取得する場合、 以下のfindを …

WYSIWYGエディタCKEditor + KCFinderで画像の保存先を動的に指定する方法。

WYSIWYGエディタCKEditor + KCFinderで画像の保存先を動的に指定する方法。

ブログの記事の編集などにWYSIWYGエディタCKEditor + KCFinderを導入しているサイトでログインするユーザごとに画像のアップロード先を切り替えたい状況がございます。 その方法を紹介し …

【WordPress】カスタムフィールドで何でも出来る!カスタムフィールドの便利な使い方 その1

【WordPress】カスタムフィールドで何でも出来る!カスタムフィールドの便利な使い方 その1

WordPressでサイトを作る場合カスタムフィールドがとても便利ですよね! そして投稿ページで入力したカスタムフィールドの値を他のトップページや固定ページでも使いたい。なんて事があるかと思います。 …

PHPフレームワークLaravel4を試しました。

PHPフレームワークLaravel4を試しました。

1. Laravel4をインストールするためには、Composerを利用します。 以下のコマンドでComposerをインストールします。 % curl -s http://getcomposer.or …