14

在 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')) );
4

4 回答 4

23

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

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

或者:

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

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

$this->view->url(...);
于 2009-11-05T14:20:06.203 回答
3

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

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

    $this->setAction($url);
    ...
}
于 2011-04-10T11:01:01.880 回答
2

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

$form = new Zend_Form;
$form->setMethod('post')->setAction( $this->getHelper('url')->url(array('controller' => 'index', 'action' => 'add')) );
于 2009-11-05T14:18:35.910 回答
1

在 zf3 中,您可以使用:

    $form = new YourFormClass();
    $form->setMethod('post')->setAction($this->url()->fromRoute(array('controller' => 'index', 'action' => 'add'));
于 2017-05-05T04:26:46.113 回答