マーカーネット株式会社

WEB制作ブログ WEBデザイン、HTMLコーディング、PHP、スマートフォン制作などWEB制作全般に役立つ情報を発信していきます。

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’と表示されます
}
}

東京のWEB制作会社 マーカーネット株式会社 公式WEBサイトへ

-PHP

関連記事

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

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

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

Laravel SocialiteでGitHubを利用した認証

Contents Lravel SocialiteでGitHubを利用したOAuthによる認証の手順を紹介します。 1. GithubでOAuthアプリケーションを作成 GitHubにログインし、 [ …

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

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

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

CakePHP2.Xで404 Not Foundエラーをリダイレクトする方法

CakePHP2.Xで404 Not Foundエラーをリダイレクトする方法

CakePHP2.Xでは、404 NOT Foundエラーが発生した場合、 デフォルトでは、 app/View/Errors/error400.ctp の内容が表示されます。 サイトによっては、 40 …

PHPの日付操作ライブラリについて

PHPの日付操作ライブラリについて

PHPで日付の操作を行うとき、PHPの標準の関数(date, mktime, strtotimeなど)を利用しますが、使い方に慣れるのがなかなか大変です。 そこでPHPで日付を操作するのに便利なライブ …