Zend_Controllerを使ったWebアプリケーションで、ルーティングを定義してみます。

chkというモジュールを作成した場合、デフォルトでは次のようなURLで、アクセスできます。

http://ドメイン/chk

これを次のようなURLでアクセスできるようにしてみます。

http://ドメイン/testchk

前回のサンプルにchkモジュールとblogモジュールを追加して動作を確認します。

ルーティング

モジュール名とモジュールを配置するディレクトリの対応は次のようにします。

モジュール名 モジュール配置ディレクトリ
default BASEDIR/webapp05/modules/default
chk BASEDIR/webapp05/modules/chk
blog BASEDIR/webapp05/modules/blog
デフォルトのルーティング

デフォルトコントローラ(index)、デフォルトアクション(index)に対応するURLは、次のようになります。

モジュール名 URL
default http://サンプル/
http://サンプル/index
http://サンプル/index/index
http://サンプル/default/index
http://サンプル/defalut/index/index
chk http://サンプル/chk
http://サンプル/chk/index
http://サンプル/chk/index/index
blog http://サンプル/blog
http://サンプル/blog/index
http://サンプル/blog/index/index

ルーティングの変更

ディスパッチループに入る前に次のようにルートをルーターに追加します。

PHP:
  1. // $frontは、フロントコントローラオブジェクト
  2.  
  3.   $router = $front->getRouter();
  4.  
  5.   // ルートを定義
  6.  
  7.   $route = new Zend_Controller_Router_Route(
  8.                                             ':controller/:action/*',
  9.                                             array(
  10.                                                   'module'=>'default',
  11.                                                   'controller'=>'index',
  12.                                                   'action'=>'index',
  13.                                                   )
  14.                                             );
  15.   $router->addRoute('default', $route);
  16.  
  17.   $route = new Zend_Controller_Router_Route(
  18.                                             'testchk/:controller/:action/*',
  19.                                             array(
  20.                                                   'module'=>'chk',
  21.                                                   'controller'=>'index',
  22.                                                   'action'=>'index',
  23.                                                   )
  24.                                             );
  25.   $router->addRoute('name_testchk', $route);
  26.  
  27.   $route = new Zend_Controller_Router_Route(
  28.                                             'blog/:controller/:action/*',
  29.                                             array(
  30.                                                   'module'=>'blog',
  31.                                                   'controller'=>'index',
  32.                                                   'action'=>'index',
  33.                                                   )
  34.                                             );
  35.   $router->addRoute('name_blog', $route);

マニュアル(7.5.3. 基本的な RewriteRouter の操作法)に、次の記述があります。

一番最後にマッチしたルートが適用されるので、 汎用的なルートは最初に定義するようにしましょう。

defaultのルートを最後に追加すると、他のモジュールへルーティングされず、エラーコントローラで処理されることになります。

変更後のルーティング

ルーティングを変更した後は、次のようなURLでモジュールにアクセスできます。

モジュール名 URL
default http://サンプル/:controller/:action
chk http://サンプル/testchk/:controller/:action
blog http://サンプル/blog/:controller/:action

:controllerはコントローラ名、:actionはアクション名です。
:controller名のデフォルトはindex、:actionのデフォルトはindexで、省略可能です。

メモ

今回変更したルートでは、defaultモジュールのルーティングが、デフォルトと違った振る舞いをします。

ルーティングの変更前は、次のURLでルーティング可能です。

http://サンプル/default/:controller/:actoin

ルーティング変更後は、このURLではアクセスできなくなります。

defaultモジュールは、次の形式でのアクセスのみとなります。
http://サンプル/:controller/:actoin

サンプルダウンロード

ダウンロードページにアーカイブしたファイルを置きました。

Tags: ,

コメントをどうぞ