ZF2重定向到控制器动作的路由并获得参数

时间:2015-03-18 18:31:05

标签: php zend-framework2

因此,我尝试将代码重定向到控制器http://localhost/salesorder/view?id=5509c273f948e7cf068b456a内的另一个操作,此视图操作正常。但每次重定向代码运行时,它都会将我重定向到http://localhost/salesorder?id=5509c273f948e7cf068b456a

我现在无能为力,我做错了什么。

$params = ['controller' => 'SalesOrder',
           'action'     => 'view',
            ];

$options = ['query' => ['id' => (string) $orderId]];

return $this->redirect()->toRoute('Salesorder', $params, $options);

我的moduleconfig看起来像这样

        'Salesorder' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/salesorder',
                'defaults' => array(
                    '__NAMESPACE__' => 'Backend\Controller',
                    'controller'    => 'SalesOrder',
                    'action'        => 'new',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type' => 'Segment',
                    'options' => array(
                        'route' => '[/:action]',
                        'constraints' => array(
                            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                        ),
                        'defaults' => array(
                            'action' => 'new'
                        ),
                    ),
                ),  
            ),
        ),  

1 个答案:

答案 0 :(得分:1)

请原谅我无法发表评论,因为我没有足够的声誉,但这可能会引导您找到解决方案:

首先修复toroute函数调用:

return $this->redirect()->toRoute('Salesorder', array('id'=> (string) $orderId));

然后将路线规范修改为:

'Salesorder' => array(
     'type' => 'segment',
     'options' => array(
         'route' => '/MODULE_NAME/SalesOrder/new/:id[/]',
         'constraints' => array(
             'id' => '[a-zA-Z0-9_-]*'
             ),
         'defaults' => array(
             'controller' => 'MODULE\Controller\SalesOrder',
             'action' => 'new',
         ),
     ),
),