LoginSignup
3
0

More than 5 years have passed since last update.

Phalconのルーティングメモ

Last updated at Posted at 2018-06-08

Phalconのルーティングを忘れないメモです。

  • Phalcon3を前提。

基本

$router->add('/', ['controller' => 'home', 'action' => 'index']); // HTTPメソッドならなんでも
$router->addGet('/', ['controller' => 'home', 'action' => 'index']); // GETだけ
$router->addPost('/', ['controller' => 'home', 'action' => 'index']); // Postだけ
$router->add('/', ['controller' => 'home', 'action' => 'index'])->via(['POST', 'PUT']); // 個別指定

Short Syntax

$router->add('/', ['controller' => 'home', 'action' => 'index']); // これを
$router->add('/', 'home::index'); // こう書くことが出来る

別名つける

$router->add('/', 'home::index')->setName('home');

// $url->get(['for' => 'home']); // /

パラメータ埋め込み

$router->add('/users/{id}', 'user::show');
$router->add('/users/{id:[0-9]+}', 'user::show'); // 正規表現で制御

// 取得の際は
// $this->dispatcher->getParam('id');
// で取得

converter

dispatcherにわたす前に変換してくれるやつ、この場合はidというパラメータをModelの取得結果を返すように変更してる

$router->add('/products/{id}', 'products::show');

$router->convert('id', function($id) {
    return Product::findFirstById($id);
});

ルーティングのグルーピング

class BlogRoutes extends \Phalcon\Mvc\Router\Group
{
    public function initialize()
    {
        $this->setPaths(
            [
                'module'    => 'blog',
                'namespace' => 'Blog\Controllers',
            ]
        );

        // All the routes start with /blog
        $this->setPrefix('/blog');

        // Add a route to the group
        $this->add('/save','blog::save');
    }
}

$router->mount(new BlogRoutes()); // マウントしないと適用されないので注意

正直

PhalconのルーティングはLaravelに比べてあきらかにしょぼい。

3
0
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
3
0