【问题标题】:Zend 2 redirect with toRoute not workingZend 2 重定向与 toRoute 不工作
【发布时间】:2015-06-05 01:22:51
【问题描述】:

为什么 Zend 2 是这样的!@#(#(!#@??

好的,所以我正在尝试让一个简单的重定向工作。我有一个名为“listitem”的控制器,带有一个名为“editlistitem”的操作。在用手雪橇敲打了几个小时之后,我终于得到了可以工作的表单和验证工作以及 Doctrine 的水合作用工作,所以我可以保存结果。

最后一步是将用户重定向到 'showlistitem' 操作,该操作包括后面的 id。 (完整的路由子路径是 'listitem/showlistitem/2' 其中 2 是我想查看的 id)

我试过了:

$this->redirect()->toRoute('/listitem/showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem/2');
$this->redirect()->toRoute('showlistitem/2');
$this->redirect()->toRoute('listitem/showlistitem', array('id' => 2));
$this->redirect()->toRoute('listitem-showlistitem', array('id' => 2));

它们都不起作用! (他们都找不到返回路线)

到控制器的路由在 modules.config.php 中,带有一个到动作的子路由。我可以通过手动输入直接转到网址,并且效果很好。如何在 哔哔声 中让 Zend 将用户从操作重定向到该路由?

【问题讨论】:

  • 倒数第二个看起来是正确的语法。如果它不起作用,您可以将您的路由配置添加到问题中吗?
  • 如果你真的想重定向到 url 然后使用 $this->redirect()->toUrl('/listitem/showlistitem/2');。否则使用@blackbishop 的答案。
  • 这是一个!@#(#(!#@??

标签: php redirect controller routes zend-framework2


【解决方案1】:

The Redirect plugin 提供的toRoute 方法需要route name 作为参数传递。这是它的描述:

toRoute(字符串 $route = null,数组 $params = array(),数组 $options = array(),布尔值 $reuseMatchedParams = false)

重定向到命名路由,使用提供的$params$options 组装URL。

鉴于这个简单的路由配置示例:

//module.config.php

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Segment',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'index',
                    'action'     => 'index',
                ),
            ),
        ),  
        'app' => array(
            'type'    => 'Literal',
            'options' => array(
                'route'    => '/app',
                'defaults' => array(
                    'controller'    => 'index',
                    'action'        => 'index',
                ),
            ),
            'may_terminate' => true,
            'child_routes' => array(
                'default' => array(
                    'type'    => 'Segment',
                    'options' => array(
                        'route'    => '/[:controller[/:action[/:id]]]',
                        'constraints' => array(
                            'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            'id'=>'[0-9]+',
                        ),
                    ),
                ),
            ),
        ),
    ),
),

此重定向有效:

return $this->redirect()->toRoute('app/default', 
           array('controller'=>'controller-name', 'action'=>'action-name', 'id'=>$id));

在你的情况下,这会工作:

return $this->redirect()->toRoute('app/default', 
            array('controller'=>'listitem', 'action'=>'showlistitem', 'id'=>2));

【讨论】:

  • 你拯救了我的一天
猜你喜欢
  • 2012-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
相关资源
最近更新 更多