### 不同路由类型
**文字原义路由 Literal routes**
* http://domain.com/blog
* http://domain.com/blog/add
* http://domain.com/about-me
* http://domain.com/my/very/deep/page
~~~
'router' => [
'routes' => [
'about' => [
'type' => \Zend\Router\Http\Literal::class,
'options' => [
'route' => '/about-me',
'defaults' => [
'controller' => 'AboutMeController',
'action' => 'aboutme',
],
],
],
],
],
~~~
**分段路由 Segment routes**
* http://domain.com/blog/1 (参数 "1" 是动态的)
* http://domain.com/news/archive/2014/january (参数 "2014" 和 "january" 都是动态的)
~~~
'router' => [
'routes' => [
'archives' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/news/archive[/:year]',
'defaults' => [
'controller' => ArchiveController::class,
'action' => 'byYear',
'year' => date('Y'),
],
'constraints' => [
'year' => '\d{4}',
],
],
],
],
],
~~~
### 不同的路由概念
**通用路由**
* `controller`
* `action` (可选的)
~~~
'router' => [
'routes' => [
'default' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/[:controller[/:action]]',
'defaults' => [
'controller' => Application\Controller\IndexController::class,
'action' => 'index',
],
'constraints' => [
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
],
],
],
],
],
~~~
**基本路由**
~~~
'router' => [
'routes' => [
'news' => [
'type' => \Zend\Router\Http\Literal::class,
'options' => [
'route' => '/news',
'defaults' => [
'controller' => NewsController::class,
'action' => 'showAll',
],
],
],
'news-archive' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/news/archive[/:year]',
'defaults' => [
'controller' => NewsController::class,
'action' => 'archive',
],
'constraints' => [
'year' => '\d{4}',
],
],
],
'news-single' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/news/:id',
'defaults' => [
'controller' => NewsController::class,
'action' => 'detail',
],
'constraints' => [
'id' => '\d+',
],
],
],
],
],
~~~
**子级路由**
~~~
'router' => [
'routes' => [
'news' => [
// First we define the basic options for the parent route:
'type' => \Zend\Router\Http\Literal::class,
'options' => [
'route' => '/news',
'defaults' => [
'controller' => NewsController::class,
'action' => 'showAll',
],
],
// The following allows "/news" to match on its own if no child
// routes match:
'may_terminate' => true,
// Child routes begin:
'child_routes' => [
'archive' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/archive[/:year]',
'defaults' => [
'action' => 'archive',
],
'constraints' => [
'year' => '\d{4}',
],
],
],
'single' => [
'type' => \Zend\Router\Http\Segment::class,
'options' => [
'route' => '/:id',
'defaults' => [
'action' => 'detail',
],
'constraints' => [
'id' => '\d+',
],
],
],
],
],
],
],
~~~
### 我们博客模块的一个应用实例
**在模块配置中设定路由**
> module/Blog/config/module.config.php
~~~
namespace Blog;
use Zend\Router\Http\Literal;
use Zend\Router\Http\Segment;
use Zend\ServiceManager\Factory\InvokableFactory;
return [
'service_manager' => [ /* ... */ ],
'controllers' => [ /* ... */ ],
'router' => [
'routes' => [
'blog' => [
'type' => Literal::class,
'options' => [
'route' => '/blog',
'defaults' => [
'controller' => Controller\ListController::class,
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'detail' => [
'type' => Segment::class,
'options' => [
'route' => '/:id',
'defaults' => [
'action' => 'detail',
],
'constraints' => [
'id' => '[1-9]\d*',
],
],
],
],
],
],
],
'view_manager' => [ /* ... */ ],
];
~~~
**添加新动作到控制器**
> module/Blog/src/Controller/ListController.php
~~~
use InvalidArgumentException;
/* ... */
public function detailAction()
{
$id = $this->params()->fromRoute('id');
try {
$post = $this->postRepository->findPost($id);
} catch (\InvalidArgumentException $ex) {
return $this->redirect()->toRoute('blog');
}
return new ViewModel([
'post' => $post,
]);
}
~~~
**创建详情模板**
> module/Blog/view/blog/list/detail.phtml
~~~
<h1>Post Details</h1>
<dl>
<dt>Post Title</dt>
<dd><?= $this->escapeHtml($this->post->getTitle()) ?></dd>
<dt>Post Text</dt>
<dd><?= $this->escapeHtml($this->post->getText()) ?></dd>
</dl>
~~~