0

我已经使用 Skeleton Application and Module 创建了一个基本的 Zend Framework 2 应用程序,尽管我遇到了路由问题并且似乎无法解决它。

在我的模块中,它松散地基于 Zend Framework 网站上的新快速入门模块,我在 module.config.php 中有以下路由

'router' => array(
    'routes' => array(
        'blog' => array(
            'type'    => 'Literal',
            'options' => array(
                // Change this to something specific to your module
                'route'    => '/blog',
                'defaults' => array(
                    // Change this value to reflect the namespace in which
                    // the controllers for your module are found
                    '__NAMESPACE__' => 'Blog\Controller',
                    'controller'    => 'View',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // This route is a sane default when developing a module;
                // as you solidify the routes for your module, however,
                // you may want to remove it and replace it with more
                // specific routes.
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '[/:controller[/:action]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                        ),
                    ),
                ),
            ),
        ),
    ),
),

使用标准模块、控制器和操作 URL 请求时,此路由似乎可以正常工作:

blah.com/blog/post/view

我遇到问题的地方是当我尝试通过 URL 传递一些数据时:

blah.com/blog/post/view/id/1

两个 URL 中的后者导致 404。我猜我需要一个子路由来允许在 URL 上传递数据,尽管我完全坚持我所需要的。有人能指出我正确的方向吗?我已经浏览了参考指南,尽管我似乎无法越界。

任何帮助表示赞赏。

4

2 回答 2

2

您必须在路线中添加可选段。还要为其添加默认值:试试这个:

'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '[/:controller[/:action[/id[/:idvar]]]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'idvar'  => '0'
                        ),
                    ),
                ),

并在您的控制器操作中,尝试:

$id = $this->params('idvar');

检索idvar值。

于 2012-11-10T16:10:29.773 回答
2

如果您需要多个可选参数并且可以以任意随机组合提供(例如 pageNumber = 2,participant = active),则需要添加查询类型的子路由,这样您就可以完全灵活地提供 GET 参数:

适合以下示例的 url 可以使用 url 视图助手使用此语法构建: $this->url('event/index/query', array('o_id' => 1, 'participation' => 'active', 'pageNumber' => '2'));

    'routes' => array(
       'event' => array(
            'type'    => 'literal',
            'options' => array(
                'route'    => '/event',
                'defaults' => array(
                    'controller' => 'cebEvent.eventController',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'index' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/index[/:o_id]',
                        'defaults' => array(
                            'controller' => 'eventController',
                            'action'     => 'index',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        'query' => array(
                            'type' => 'Query',
                        ),
                    ),

                ),
于 2012-11-11T11:43:35.533 回答