Zend Form SetAction 使用命名路由

发布于 2024-09-10 18:00:48 字数 962 浏览 6 评论 0原文

我有一个表格,我正在尝试为其设置操作。我想使用我在引导程序中创建的路由在表单文件(扩展 Zend_Form)中而不是在控制器或视图中声明操作。 通常,当我想使用路线时,我会

$this->url(array(), 'route-name');

在视图或

$this->_helper->url(array(), 'route-name');

控制器中执行类似的操作。

如何从 Zend_Form 中调用路线?


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

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

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

和在视图文件中:

<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>

I have a form that I am trying to set the action for. I want to declare the action inside my form file (which extends Zend_Form) instead of in a controller or view, using a route I have created in my bootstrap.
Usually when I want to use a route I do something like

$this->url(array(), 'route-name');

in the view, or

$this->_helper->url(array(), 'route-name');

in the controller.

How do I call a route from within Zend_Form?


edit:
I have given up trying to load a route into zend_form. Perhaps in a future release there may be a function to easily do this?

I have created a viewScript for my form and set the route in that:
In the form init function:

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

and in the view file:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

成熟的代价 2024-09-17 18:00:48

方法 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);
    ...
}

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

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

我更喜欢方法 1。它更详细,但表单中的依赖项少了一个。

Method 1: Get the router

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

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

Method 2: Get an instance of the View object and call the url-view-helper directly

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

I prefer Method 1. It is more verbose but you have one dependency less in your form.

究竟谁懂我的在乎 2024-09-17 18:00:48

如果在我的控制器操作中:

$this->view->form = $form;

我将使用视图助手 url 在我的视图脚本 (xxx.phtml) 中生成表单操作 url:

$url = $this->url(array('controller'=>'my-controller-name', 
                    'action'=>'my-action-name'), 
              'my-route-name'
             );

$this->form->setAction($url);

echo $this->form;

if in my controller action:

$this->view->form = $form;

I will use view helper url to generate the form action url in my view script ( xxx.phtml):

$url = $this->url(array('controller'=>'my-controller-name', 
                    'action'=>'my-action-name'), 
              'my-route-name'
             );

$this->form->setAction($url);

echo $this->form;
初熏 2024-09-17 18:00:48

现在,您可以通过 Zend_Form 类上的 getView() 方法访问 Zend_View 对象:

// init your form    
public function init()
{
    $view = $this->getView();
    $url = $view->url(array('module'=>'login','action'=>'login'));
    $this->setAction($url);
    ...
}

这可能对 ZF 1.8+ 有帮助

Nowadays you can access the Zend_View object via getView() method on Zend_Form classes:

// init your form    
public function init()
{
    $view = $this->getView();
    $url = $view->url(array('module'=>'login','action'=>'login'));
    $this->setAction($url);
    ...
}

Might this help on ZF 1.8+

机场等船 2024-09-17 18:00:48

我不知道它是什么时候添加的,但有一个更简单的解决方案。

您可以使用 getView() 检索表单的视图对象,该对象可以访问已注册的路由。

//In the form
$this->setAction($this->getView()->url(array('param1' => 'value1'), 'routeName'));

I do not know when it was added, but there is an even simpler solution.

You can retrieve the form's view object with getView(), which has access to the registered routes.

//In the form
$this->setAction($this->getView()->url(array('param1' => 'value1'), 'routeName'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文