Yaf_Route_Regex::__construct
(Yaf >=1.0.0)
Yaf_Route_Regex::__construct — Yaf_Route_Regex のコンストラクタ
説明
public Yaf_Route_Regex::__construct
( string $match
, array $route
[, array $map
[, array $verify
[, string $reverse
]]] )
パラメータ
match
正規表現のパターン。リクエストの URI とマッチさせます。 もしマッチしなければ Yaf_Route_Regex は
false
を返します。route
リクエスト URI がパターンにマッチしたときに、 Yaf_Route_Regex はこれを使ってルーティング先を判断します。
この配列で指定するモジュール、コントローラ、アクションはすべて任意指定で、 値を省略したときにはデフォルト設定を利用します。
map
マッチした結果に代入する名前の配列。
verify
reverse
URL を組み立てるときに使う文字列。 Yaf_Route_Regex::assemble() を参照ください。
注意:
このパラメータは 2.3.0 で導入されました。
返り値
例
例1 Yaf_Route_Regex() の例
<?php /** * Add a regex route to Yaf_Router route stack */ Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name", new Yaf_Route_Regex( "#^/product/([^/]+)/([^/])+#", //match request uri leading "/product" array( 'controller' => "product", //route to product controller, ), array( 1 => "name", // now you can call $request->getParam("name") 2 => "id", // to get the first captrue in the match pattern. ) ) );?>
例2 Yaf_Route_Regex() (2.3.0 以降) の例
<?php /** * Use match result as MVC name */ Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name", new Yaf_Route_Regex( "#^/product/([^/]+)/([^/])+#i", //match request uri leading "/product" array( 'controller' => ":name", // route to :name, which is $1 in the match result as controller name ), array( 1 => "name", // now you can call $request->getParam("name") 2 => "id", // to get the first captrue in the match pattern. ) ) );?>
例3 Yaf_Route_Regex と名前付きキャプチャグループ (2.3.0 以降) の例()example
<?php /** * Use match result as MVC name */ Yaf_Dispatcher::getInstance()->getRouter()->addRoute("name", new Yaf_Route_Regex( "#^/product/(?<name>[^/]+)/([^/])+#i", //match request uri leading "/product" array( 'controller' => ":name", // route to :name, // which is named capture group 'name' in the match result as controller name ), array( 2 => "id", // to get the first captrue in the match pattern. ) ) );?>
例4 Yaf_Route_Regex() の例
<?php /** * Add a regex route to Yaf_Router route stack by calling addconfig */ $config = array( "name" => array( "type" => "regex", //Yaf_Route_Regex route "match" => "#(.*)#", //match arbitrary request uri "route" => array( 'controller' => "product", //route to product controller, 'action' => "dummy", //route to dummy action ), "map" => array( 1 => "uri", // now you can call $request->getParam("uri") ), ), ); Yaf_Dispatcher::getInstance()->getRouter()->addConfig( new Yaf_Config_Simple($config));?>
参考
- Yaf_Router::addRoute() - 新しいルートをルーターに追加する
- Yaf_Router::addConfig() - 設定で定義したルートをルーターに追加する
- Yaf_Route_Static
- Yaf_Route_Supervar
- Yaf_Route_Simple
- Yaf_Route_Rewrite
- Yaf_Route_Map