LoginSignup
13
13

More than 5 years have passed since last update.

Phalcon のルーティングは「最後が勝つ」

Last updated at Posted at 2013-10-28

Phalcon PHP Framework で、優先度が影響するルーティングが必要になったときに少しつまづいたのでメモ。

Phalcon のルーティングは、どうやら記述したのと逆順に評価されるようです。
たとえば次のように、トップレベルに固定ではないページを配置しようとした場合、

  • example.com/
  • example.com/about
  • example.com/<ページID:アルファベット>

Symfony2 では次のように、評価してほしい順番に記述します。

routing.yml
top:
    pattern: /
    defaults: { _controller: DemoBundle:Index:top }

about:
    pattern: /about
    defaults: { _controller: DemoBundle:Index:about }

flexpage:
    pattern: /{page_id}
    defaults: { _controller: DemoBundle:Index:flexpage }

しかし Phalcon では、次のように記述してしまうと、/ (トップページ) にアクセスしても /about にアクセスしても IndexController::flexpageAction() が呼び出されてしまいます。

routing.phpダメなやつ
$router = new Palcon\Mvc\Router(false);

$router->add('/', 'Index::top');
$router->add('/about', 'Index::about');
$router->add('/{page_id}', 'Index::flexpage'); // ← "/", "/about" がマッチしちゃう

/{page_id} のルールを最初に持ってくれば、正しく /, /about にアクセスすることができるようになります。

routing.phpグッボーイ!
$router = new Palcon\Mvc\Router(false);

$router->add('/{page_id}', 'Index::flexpage');
$router->add('/', 'Index::top');              // ←YES!
$router->add('/about', 'Index::about'); // ←YES!!

phalcon routing priority とかでググっても見つけられなかったので検索用
phalcon ルーティング 評価 順番
参考: router.c

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13