zend框架2路由转向不正确的操作

时间:2016-11-23 09:35:54

标签: php routing zend-framework2

我正在尝试使用sID和cID参数执行数据表操作。

/的CustomerManager /数据表/一百分之一百二十三

当上面的网址被点击时,它会转到refreshdata,见下图。 enter image description here

当网址

时工作正常

/的CustomerManager /数据表

任何人都可以看到配置问题吗?

'CustomerManager' => array(
            'type' => 'Segment',
            'options' => array(
                'route'    => '/CustomerManager[/:action]',
                'constraints' => array(
                    'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                ),
                'defaults' => array(
                    'controller' => 'CustomerManager\Controller\CustomerManager',
                    'action'     => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'datatable' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route' => '/[:sID][/:cID]',
                        'constraints' => array(
                            'cono' => '[a-zA-Z0-9_-]+',
                            'cust' => '[a-zA-Z0-9_-]+',
                        ),
                        'defaults' => array(
                            'action' => 'datatable',
                        ),
                    ),
                ),
                'refreshdata' => array(
                    'type' => 'segment',
                    'options' => array(
                        'route'    => '/[:showDel][/:sID][/:cID]',
                        'defaults' => array(
                            'action' => 'refreshdata'
                        ),
                        'constraints' => array(
                            'showDel' => '[a-zA-Z0-9_-]+',
                            'sID' => '[a-zA-Z0-9_-]+',
                            'cID' => '[a-zA-Z0-9_-]+',
                        )
                    )
                ),
            )

2 个答案:

答案 0 :(得分:2)

似乎“refreshdata”覆盖“datatable”,因为你期望一个带有最后3个参数的url,它们都可以是字母或数字

并在请求中使用两个与“refreshdata”匹配的数字类型参数调用url

我也认为

'cono' => '[a-zA-Z0-9_-]+',
'cust' => '[a-zA-Z0-9_-]+',

在这一行中“cono”和“cust”必须更改为“sID”和“cID”

答案 1 :(得分:2)

在我发表评论后,我们举例说明了如何宣布您的路线更加结构化:

'router' => array(
    'CustomerManager' => array(
        'CustomerManager' => array(
            'type' => 'literal',
            'options' => array(
                'route' => '/CustomerManager',
                'defaults' => array(
                    'controller' => 'Application\Controller\IndexController',
                    'action' => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                // Literal route for data table
                'datatable' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => '/datatable',
                        'defaults' => array(
                            'action' => 'datatable',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        // Child route for table cell
                        'cell' => array(
                            'type' => 'segment',
                            'options' => array(
                                'route' => '/[:sID][/:cID]',
                                'constraints' => array(
                                    'sID'=> '[a-zA-Z0-9_-]+',
                                    'cID' => '[a-zA-Z0-9_-]+',
                                ),
                            ),
                        ),
                    ),
                ),
                // Literal route for refreshtable
                'refreshtable' => array(
                    'type' => 'literal',
                    'options' => array(
                        'route' => '/refreshtable',
                        'defaults' => array(
                            'action' => 'refreshtable',
                        ),
                    ),
                    'may_terminate' => true,
                    'child_routes' => array(
                        // Child route for table cell
                        'cell' => array(
                            'type' => 'segment',
                            'options' => array(
                                'route' => '/[:sID][/:cID]',
                                'constraints' => array(
                                    'sID'=> '[a-zA-Z0-9_-]+',
                                    'cID' => '[a-zA-Z0-9_-]+',
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

这只是一个示例,您可以根据具体需要对其进行更改。

修改

了解路由的基础知识对于构建正确的应用程序非常重要。你应该仔细阅读ZF2 Routing documentation,你需要的所有信息都在那里......

例如:

在问题中,sIDcID段是可选的(方括号之间)。这很可能也不会像您期望的那样起作用。给出列cID但未给出sID值时可能会出现问题。在这种情况下,路由器会将cID值附加到sID参数名称,从而导致匹配中出现意外的路由参数值。

这样的事情可以通过你的约束来做这样的事情来解决:

'constraints' => array(
    'sID' => 's[a-zA-Z0-9_-]+',
    'cID' => 'c[a-zA-Z0-9_-]+' 
),

通过将正则表达式约束(以及网址中的值)与sc相加,您可以确保将正确的值与正确的参数绑定。