zf2.2配置路由规则示例

1 配置路由规则的文件位置:

module/moduleName/config/module.confg.php

2 zf2.2的路由类型:

8种,分别为Hostname, Literal, Method, Part, Regex, Scheme, Segement, Query

示例一 : 一个名为'album'、类型为'segment',它允许我们在URL pattern中指定占位符

注:

(1) []表示其中的内容是可选的

(2) :action表示这是一个变量,后面讲可以对其定义,如: 'action' => '[a-zA-Z0-9_-]*'

(3)下面几种示例是该路由规则可以通过的

1>http://localhost/zend/album

2>http://localhost/zend/album/add

3>http://localhost/zend/album/edit/1

'router' => array(

    'routes' => array(

        'album' => array(

            'type' => 'segment',
            'options' => array(
                'route' => '/album[/:action][/:id]',
                'constraints' => array(
                     'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                      'id' => '[0-9]+'
                 ),
                'defaults' => array(
                    'controller' => 'Album\Controller\Album',
                    'action' => 'index',
                ),
            ),
        ),       
    ),
),

示例二: 类型为Literal的简单路由配置信息,未设置路由名称

'router' => array(
    'routes' => array(
        'type' => 'Literal',
        'options' => array(
            'route' => '/hello/world',
            'defaults' => array(
                'controller' => 'Test\Controller\Hello',
                'action' => 'world',
            ),
        ),
    ),
),     

示例三:两个Literal类型的路由

'router' => array(
    'routes' => array(
        //Literal route namede 'home'
        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),

        //Literal route named 'contact'
        'contact' => array(
            'route' => '/contact',
            'options' => array(
                'controller' => 'Application\Controller\Contact',
                 'action' => 'form',
            ),
        ),
    ),
),

示例四: 子路由 child_routes

注: 名为home的路由指定的是整个工程的首页

' router' => array(
   ' routes' => array(

        //Literal route named 'home'
        'home' => array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/',
                'defaults' => array(
                    'controller' => 'Application\Controller\Index',
                    'action' => 'index',
                ),
            ),
        ),

        //Literal route named 'blog', with child routes
        'blog' =>array(
            'type' => 'Literal',
            'options' => array(
                'route' => '/blog',
                'defaults' => array(
                    'controller' => 'Application\Controller\Blog';
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                //segment route for viewing one blog post
                'post' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/[:slug]',
                        'constraints' => array(
                             'slug' => '[a-zA-Z0-9_-]+',
                        ),
                        'defaults' => array(
                            'action' => 'view',
                        ),
                    ),
                ),

                //Literal doute for viewing RSS feed
                'rss' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route' => '/rss',
                        'defaults' => array(
                            'action' => 'rss'
                        ),
                    ),
                ),
            ),
        ),
    ),
),

你可能感兴趣的:(zf2.2配置路由规则示例)