【问题标题】:Zend Framework - Pass variable from one controller to another on redirectZend Framework - 在重定向时将变量从一个控制器传递到另一个控制器
【发布时间】:2011-03-17 15:34:14
【问题描述】:

我在 Zend Framework 中有一个小表单,当它发送并生效时,我想将 $id 传递给另一个控制器,在那里我将在另一个视图中显示有关该 id 的一些信息。

表单和验证已完成,但我似乎无法弄清楚如何将 $id 传递给另一个控制器。

我有以下代码:

if ($this->_request->isPost()) 
    {       
        $formData = $this->_request->getPost(); 
        if ($form->isValid($formData)) {
            die($id);
        } else {
            $form->populate($formData);
        }
    }

现在它在有效时显示 id,但我想用 $id 变量将它重定向到 formAction。

谢谢!

【问题讨论】:

    标签: php zend-framework


    【解决方案1】:

    如果它只有 1 个变量,您可以简单地将其作为 GET 请求发送。所以

    $this->_redirect('/controller/action/varname/varvalue')
    

    会起作用的。在控制器中,您可以使用

    检索它
    $myvar = $this->_getParam('varname', false);
    

    您可以使用多个变量执行此操作,只需将名称/值对附加到 URL。

    另一方面,如果您有大量数据,那么您可能需要使用 cURL POST 或使用 Zend Session 将其存储在控制器 A 中,然后在控制器 B 中检索

    【讨论】:

    • 它只有 1 个变量,所以我会试试这个。谢谢!
    • 它似乎不起作用,我尝试了你的代码,但回显时我什么也没得到。
    • 你能把你的页面重定向到的 URL 粘贴到这里吗?例如,url 应该看起来像这样 http://mywebapp-dev.com/search/q/zend 。在这里,如果你做了$this->_getParam('q', false),你会得到zend
    • /index/bon/64/ 是 URL,当我回显 $this->_getParam('bon', false);我什么都得不到……
    • 如果你在 indexController 中访问你的 index 方法,那么它必须是 /index/index/bon/64 除非你使用自定义路由。
    【解决方案2】:

    如果您不想使用_forward,我建议您使用Zend_Session 将有问题的ID 放入会话中,或者根据您的应用程序的内部结构,使用Zend_Registry::set('variablename', $id); 在注册表中

    如果您可以使用_forward,那么您应该能够从另一个控制器中的请求对象获取数据,就像您在这个控制器中所做的那样。

    更新:

    Forward,将您的呼叫转发到指定的控制器和操作。 转发到另一个控制器/动作。

    有点像$this->_forward('nameOfAction', 'nameOfController', 'module', paramsArray)。必须给出动作,但所有其他参数都是可选的。如果需要,paramsArray 可用于传递数据。

    以下内容摘自关于_forward的源文档

    /**
     * Forward to another controller/action.
     *
     * It is important to supply the unformatted names, i.e. "article"
     * rather than "ArticleController".  The dispatcher will do the
     * appropriate formatting when the request is received.
     *
     * If only an action name is provided, forwards to that action in this
     * controller.
     *
     * If an action and controller are specified, forwards to that action and
     * controller in this module.
     *
     * Specifying an action, controller, and module is the most specific way to
     * forward.
     *
     * A fourth argument, $params, will be used to set the request parameters.
     * If either the controller or module are unnecessary for forwarding,
     * simply pass null values for them before specifying the parameters.
     *
     * @param string $action
     * @param string $controller
     * @param string $module
     * @param array $params
     * @return void
     */
    

    因此,如果您要在控制器 bar 中转发操作 foo,您可以这样做

    $this->_forward('foo', 'bar');
    

    BarController

    public function fooAction() {
      // Get the request and continue to work with it here
      $request = $this->_request();
      ...
    }
    

    【讨论】:

    • _forward 是内部的 - 它不会触发新请求。它欺骗 ZF 认为转发的模块/控制器/动作三元组是原始的;)
    • 谢谢,但是如何在 paramsArray 中传递变量?因为现在我有了这个: $this->_forward('bon', 'index', null, 'id => $id');但这当然行不通……
    【解决方案3】:

    有一些选项可以做到这一点(主要使用 Zend_Session),但是需要这种设计的味道很糟糕。你到底想用这个做什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多