Zend Form SetAction使用命名路由

Ric*_*ing 10 zend-framework zend-form zend-route

我有一个表单,我正在尝试设置该操作.我想使用我在引导程序中创建的路径,在我的表单文件(扩展Zend_Form)中而不是在控制器或视图中声明操作.通常当我想使用路线时,我会做类似的事情

$this->url(array(), 'route-name');
Run Code Online (Sandbox Code Playgroud)

在视图中,或

$this->_helper->url(array(), 'route-name');
Run Code Online (Sandbox Code Playgroud)

在控制器中.

如何从Zend_Form中调用路由?


编辑:我已经放弃了尝试将路由加载到zend_form.也许在将来的版本中可能有一个功能可以轻松地做到这一点?

我已经为我的表单创建了一个viewScript并在其中设置了路径:在表单init函数中:

$this->setDecorators(array(
    'PrepareElements',
        array(
            'ViewScript', array(
                    'viewScript' => 'forms/formView.phtml'
            ))));
Run Code Online (Sandbox Code Playgroud)

并在视图文件中:

<form method="post" action="<?php echo $this->url(array(), 'route-name'); ?>" enctype="application/x-www-form-urlencoded">
    <?php
        foreach ($this->element->getElements() as $element)
        {
            echo $element;
        }
    ?>
</form>
Run Code Online (Sandbox Code Playgroud)

Ben*_*mer 18

方法1:获取路由器

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

    $this->setAction($url);
    ...
}
Run Code Online (Sandbox Code Playgroud)

方法2:获取View对象的实例并直接调用url-view-helper

// in your form    
public function init()
{
    $url = Zend_Layout::getMvcInstance()->getView()->url(array(), 'routeName';
    $this->setAction($url);
    ...
}
Run Code Online (Sandbox Code Playgroud)

我更喜欢方法1.它更详细,但你的形式有一个较少的依赖.