Phalcon#基本功能#路由

Phalcon\Mvc\Router 提供了高级路由支持,在 MVC 模式下可以自定义路由映射到指定的控制器和动作。Router 只是收集路由信息,最终是通过派遣器 Phalcon\Mvc\Dispatcher 来调用相应控制器和动作。

注册路由 router 和 派遣器 dispatcher

set('router', function () {
    $router = Phalcon\Mvc\Router();
    $router->add(
        '/{id:[0-9]+}',
        [
           'controller' => 'index',
           'action' => 'show'
        ]
    );
    return $router;
});

//注册派遣器
$di->set('dispatcher', function () {
    $dispatcher = new Phalcon\Mvc\Dispatcher();
    $dispatcher->setDefaultNamespace('App\Controllers');
    return $dispatcher;
});

默认 URI 信息是从 $ _GET['_url'] 获得,也可以设置为从 $_SERVER['REQUEST_URI'] 获取。
使用 $_GET['_url'] (默认):

use Phalcon\Mvc\Router;
$router->setUriSource(Router::URI_SOURCE_GET_URL);

Nginx 配置:

location / {
    try_files $uri $uri/ /index.php?_url=$uri&$args;
}

使用 $_SERVER['REQUEST_URI']:

use Phalcon\Mvc\Router;
$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI);

Nginx 配置:

location / {
    try_files $uri $uri/ /index.php;
}
定义单个路由
$router->add(
    '/article/info', 
    [
        'controller' => 'article',
        'action' => 'info'
    ]
);
使用通配符定义多个路由
$router->add(
    'admin/:controller/xxx/:action/:params',
    [
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ]
);

通过上面的通配符定义了一个可以匹配多个 URL 的路由,当访问这个 URL(/admin/news/xxx/show/1/test),则对应的参数如下:
Controller:NewsController
Action:showAction
Parameter:1
Parameter:test

支持的通配符:

Placeholder Regular Exception Usage
/:module /([a-zA-Z0-9_-]+) 匹配模块
/:controller /([a-zA-Z0-9_-]+) 匹配控制器
/:action /([a-zA-Z0-9_]+) 匹配方法
/:params (/.*)* 匹配参数,可匹配多个参:/param1/param2[...]
/:namespace /([a-zA-Z0-9_-]+) 匹配命名空间
/:int /([0-9]+) 匹配数字参数
参数名称
$router->add(
    '/news/([0-9]{4})/([0-9]{2})/([0-9]{2})/:params',
    [
         'controller' => 'news',
         'action' => 'show',
         'year' => 1,     // ([0-9]{4})
         'month' => 2,    // ([0-9]{2})
         'day' => 3,      // ([0-9]{2})
         'params' => 4    // :params
    ]
);

控制器中获取参数:

dispatcher->getParam('year');
           
         // 获取参数month
         $month = $this->dispatcher->getParam('month');
         // 获取参数day
         $day = $this->dispatcher->getParam('day');
     }
}
短语法

下面两种路由添加方式效果相同:

add('/news/{id:[0-9]+}', 'News::show');
 
// 数组语法
$router->add(
    '/news/([0-9]+)',
    [
         'controller' => 'news',
         'action' => 'show',
         'id' => 1
    ]
);

混合使用数组和短语法:

add(
    '/news/{param1:[a-z]{2}}/([a-z]+)/([a-z\-]+)',
    [
        'param2' => 2,     // 匹配起始位置为2,第一个匹配位已被param1占用
        'param3' => 3
    ]
);
RESTful路由
addGet('/products/{id}/edit', 'Products::edit');
method path description Controller::action
GET /article 索引、列表 Article::index
GET /article/create 创建 Article::create
POST /article 保存 Article::save
GET /article/{id} 显示 Article::show
GET /article/{id}/edit 编辑 Article::edit
PUT/PATCH /article/{id} 更新 Article::update
DELETE /article/{id} 删除 Article::destroy
路由分组

当我们需要配置相同路径下的多个路由时,就可以使用路由分组的功能。

 'blog',
         'controller' => 'index'
     ]
);

// 设置路由前缀
$blog->setPrefix('/blog');

// 添加路由
$blog->add('/save', ['action' => 'save']);
$blog->add('/edit/{id}', ['action' => 'edit']);
  
// 绑定分组路由
$router->mount($blog);
路由命名

给路由命名:

add('/posts/{year}/{title}', 'Posts::show')->setName('postsShow');

生成 url 时:

get([
    'for' => 'postsShow',
    'year' => '2016',
    'title' => 'test'
]);
设置默认路由及 404 页面
add(
    '/',
    [
        'controller' => 'index',
        'action' => 'index'
    ]
);
 
// 设置404路由
$router->notFound(
    'controller' => 'index',
    'action' => 'route404'
);

你可能感兴趣的:(Phalcon#基本功能#路由)