php Zend 控制器中 url() 辅助函数的等价物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1680833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:29:40  来源:igfitidea点击:

Equivalent of url() helper function in Zend controller

phpzend-frameworkzend-form

提问by Mathew Attlee

In the Zend view helper, there is the function url() for outputting a URL based on the routing tables eg

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

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

How can I do the same thing in a controller? In particular I want to set the action URL for a Zend Form using controller/action syntax rather than a standard URL eg

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

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

回答by Ferdinand Beyer

There is an action helper for this: Zend_Controller_Action_Helper_Url. Inside an action controller, you can access it using:

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

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

or:

或者:

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

Alternatively, you can also use the view helper:

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

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

回答by Attilio

I've actually found out that only this works:

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

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

    $this->setAction($url);
    ...
}

回答by Mathew Attlee

Was able to answer my own question as it seems the following code does the trick:-

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

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

回答by Andrii Krot

In zf3 you can use:

在 zf3 中,您可以使用:

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