【问题标题】:Equivalent of url() helper function in Zend controllerZend 控制器中 url() 辅助函数的等价物
【发布时间】:2009-11-05 14:13:56
【问题描述】:

在 Zend 视图助手中,有一个函数 url() 用于根据路由表输出 URL,例如

$this->url(array('controller' => 'comments', 'action' => 'add')

如何在控制器中做同样的事情?特别是我想使用控制器/动作语法而不是标准 URL 为 Zend 表单设置动作 URL,例如

$form = new Zend_Form;
$form->setMethod('post')->setAction( $this->url(array('controller' => 'comments', 'action' => 'add')) );

【问题讨论】:

    标签: php zend-framework zend-form


    【解决方案1】:

    为此有一个操作助手:Zend_Controller_Action_Helper_Url。在动作控制器中,您可以使用以下方式访问它:

    $this->_helper->url($action [, $controller [, $module [, $params]]]);
    

    或:

    $this->_helper->url->url(array(...));
    

    或者,您也可以使用视图助手:

    $this->view->url(...);
    

    【讨论】:

    • 如果我在 IndexController(这里是 AjaxController)之外调用 $this->_helper->url(array('controller' => 'index', 'action' => 'download')),它会返回 '/ajax/Array' 我做错了什么?或者它是一个错误?
    • 使用$this->_helper->url('download', 'index')$this->_helper->url->url(array('controller' => 'index', 'action' => 'download'))。我将更新我的答案并添加指向 API 文档的链接。
    【解决方案2】:

    我实际上发现只有这样才有效:

    // in your form
    public function init()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $url = $router->assemble(
            array(
                'paramterName0' => 'parameterValue0',
                'paramterName1' => 'parameterValue1',
            ),
            'routeName'
        );
    
        $this->setAction($url);
        ...
    }
    

    【讨论】:

      【解决方案3】:

      能够回答我自己的问题,因为下面的代码似乎可以解决问题:-

      $form = new Zend_Form;
      $form->setMethod('post')->setAction( $this->getHelper('url')->url(array('controller' => 'index', 'action' => 'add')) );
      

      【讨论】:

      • 我现在使用$this->view->url(array('controller' => 'index', 'action' => 'download')),不太好,但可以在控制器内部使用。
      【解决方案4】:

      在 zf3 中你可以使用:

          $form = new YourFormClass();
          $form->setMethod('post')->setAction($this->url()->fromRoute(array('controller' => 'index', 'action' => 'add'));
      

      【讨论】:

        猜你喜欢
        • 2017-02-20
        • 2015-11-09
        • 1970-01-01
        • 1970-01-01
        • 2011-09-20
        • 2017-10-12
        • 2017-03-18
        • 2018-10-07
        • 2013-12-22
        相关资源
        最近更新 更多