【问题标题】:Zend Framework 2, redirecting to route and passing a variable to new controllerZend Framework 2,重定向到路由并将变量传递给新控制器
【发布时间】:2014-07-04 08:41:03
【问题描述】:

我在 Zend Framework 2 中实现了一个用于下新订单的表单,在提交表单后,我应该重定向到另一个路由并在另一个控制器中获取 orders.id 变量。

我尝试使用$this->redirect()->toRoute('confirm', array('param'=>$orderId));,但它根本不起作用。

也许我不知道如何在另一个 confirmAction 控制器中获取该参数。

请给我一些例子。非常感谢。

【问题讨论】:

  • 说它“不工作”并不能告诉我们代码中实际发生了什么。猜测您在重定向调用中缺少return,它应该是return $this->redirect()->toRoute('confirm', array('param'=>$orderId));,但这只是猜测。如果您希望获得更多帮助,请提供 confirm 路由的路由配置以及包含重定向的控制器操作代码部分。
  • 它是否使用orders.id 重定向?如果是这样,那么 $this->Params('param') 将为您提供另一个控制器的 id。如果没有使用参数重定向,那么您需要在 module.config.php 中设置路由

标签: php forms redirect routes zend-framework2


【解决方案1】:

1) 由于这是一个路由问题,请在 module.config.php 文件中显示您对路由的了解。如果我不得不猜测,您可能没有在配置中正确配置“参数”约束。

它应该看起来像这样:

'confirm' => array(
    'type' => 'segment',
    'options' => array(
        'route'       => '/controller_name/confirm[/][:param][/]',
        'constraints' => array(
            'param' => '[0-9]*'
        ),

        'defaults' => array(
            '__NAMESPACE__' => 'your_namespace', // ex. Application\Controller
            'action'        => 'confirm', // or whatever action you're calling
            'controller'    => 'controller_name' // ex.
        ),
    ),
),

【讨论】:

  • Yessss.... :) 非常感谢... :) 它现在正在工作。而不是使用'type' => 'literal',我应该使用'segment' + 'constraints'。再次感谢您...:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多