Zend Framework 2中的路由

Fab*_*nig 7 php zend-framework2

我正在尝试在Zend Framework 2中做一些路由,但它不起作用.

骨架应用程序的基础工作正常,所以我在文件\ module\User\config\module.config.php中添加了一个名为User的新模块和以下代码

'controllers' => array(
    'invokables' => array(
        'User\Controller\User' => 'User\Controller\UserController',
    ),
),

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

        'login' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/login',
                'defaults' => array(
                    '__NAMESPACE__' => 'User\Controller',
                    'controller'    => 'User',
                    'action'        => 'login',
                ),
            ),
        ),

        'user_create' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/user/create',
                'defaults' => array(
                    '__NAMESPACE__' => 'User\Controller',
                    'controller'    => 'User',
                    'action'        => 'create',
                ),
            ),
        ),

    ),
),
Run Code Online (Sandbox Code Playgroud)

如果我尝试访问第一个路由(/ login),它可以工作.

但第二个路径(/ user/create)导致错误:

文件:

F:\www\ZendVendas\library\Zend\Mvc\Router\Http\TreeRouteStack.php:313
Run Code Online (Sandbox Code Playgroud)

信息:

Route with name "create" not found
Run Code Online (Sandbox Code Playgroud)

如果我创建一个没有控制器名称的路由,它可以工作:

        'create' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/create',
                'defaults' => array(
                    '__NAMESPACE__' => 'User\Controller',
                    'controller'    => 'User',
                    'action'        => 'create',
                ),
            ),
        ),
Run Code Online (Sandbox Code Playgroud)

但我希望路由是"/ user/create",而不是"/ create".

我搜索了很多主题,但看不出我的错误在哪里.感谢任何帮助;)

编辑:带有@Jurian建议的调整代码

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

        'user' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/user',
                'defaults' => array(
                    'controller' => 'User\Controller\User',
                    'action'     => 'profile',
                ),
            ),


            'child_routes' => array(
                'login'  => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route'    => '/login',
                        'defaults' => array(
                            'controller' => 'User\Controller\User',
                            'action'     => 'login',
                        ),
                    ),
                ),
                'create' => array(
                    'type' => 'Literal',
                    'options' => array(
                        'route'    => '/create',
                        'defaults' => array(
                            'controller' => 'User\Controller\User',
                            'action'     => 'create',
                        ),
                    ),
                ),
            ),
        ),

    ),
),
Run Code Online (Sandbox Code Playgroud)

Jur*_*man 16

您必须了解路由在Zend Framework 2中的工作原理.路由具有名称和一些配置.结构如下:

'router' => array(
  'routes' => array(
    'route_name_1' => array( /* config here */ ),
    'route_name_2' => array( /* config here */ ),
    'route_name_3' => array( /* config here */ ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

这里的路由名称是route_name_1等等.如果您组装了一个URL,则使用该路由名称.所以如果route_name_1有url /foo/bar/baz,你可以route_name_1通过使用url视图帮助器来请求url:

echo $this->url('route_name_1'); // prints /foo/bar/baz
Run Code Online (Sandbox Code Playgroud)

您的URL /user/create被映射到路由名称, user_create因此要组合此URL,您需要传递路由名称:

echo $this->url('user_create'); // prints /user/create
Run Code Online (Sandbox Code Playgroud)

儿童路线

还有儿童路线的概念.这可以为您提供一个user映射到的路径,/user然后该用户路由有一个子create映射到/create并且因此创建的"总"路由/user/create.这可以配置如下:

'router' => array(
  'routes' => array(
    'route_name_1' => array( /* config here */ ),
    'route_name_2' => array(
      /* config here */

      'child_routes' => array(
        'child_name_1' => array( /* config here */ ),
        'child_name_2' => array( /* config here */ ),
      ),
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

现在,如果你想为route_name_2它组装一个url,看起来如上所示:

echo $this->url('route_name_1');
Run Code Online (Sandbox Code Playgroud)

但是如果你需要为child_name_1你构建一个url来构建一个/名字和它的父名之间的"路径" :

echo $this->url('route_name_1/child_name_1');
Run Code Online (Sandbox Code Playgroud)

因此,尽管您可以使用/user/create已有的路径名称访问路线,但您可能希望使用子路径,因为这样可以为您提供更灵活的路径系统:

'router' => array(
  'routes' => array(
    'user' => array(
      'type'    => 'Literal',
      'options' => array(
        'route'    => '/user/create',
          'defaults' => array(
            'controller' => 'User\Controller\User',
            'action'     => 'profile',
          ),
        ),
      ),

      'child_routes' => array(
        'login'  => array(
          'type' => 'Literal',
          'options' => array(
            'route'    => '/login',
            'defaults' => array(
              'action'     => 'login',
            ),
          ),
        ),
        'create' => array(
          'type' => 'Literal',
          'options' => array(
            'route'    => '/create',
            'defaults' => array(
              'action'     => 'create',
            ),
          ),
        ),
      ),
    ),
  ),
),
Run Code Online (Sandbox Code Playgroud)

然后你有一个user映射到"个人资料" 的路线.如果你组装user/create你去/user/create,它使用用户控制器的"createAction".同样hapens与user/login途径.