本文介绍了Zend_Form设置操作-在子目录中内置的Zend应用程序可以使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在本地服务器的子目录中构建,

My application is being built in a subdirectory of my local server, that looks something like this:

http://localhost/application/

问题是当我将表单操作设置为 / form / save时,该表单将路由到 localhost / form / save,该主机不存在。

The problem is that when I set the form action to "/form/save", the form routes to "localhost/form/save", which doesn't exist.

如果我将表单操作设置为 / application / form / save,则会收到错误消息,因为Zend使用其Front_Controller中的URL输入来选择模块,用于请求的控制器和操作。

If I set the form action to "/application/form/save", I get an error, because Zend uses the URL input in its Front_Controller to pick the modules, Controllers, and Actions to use for requests.

是否有一种简单的方法可以将base_url解析为请求?由于某种原因,它除了形式以外还可以在其他所有东西上使用。

Is there a simple way to have the base_url parsed out of requests? It works on everything else except for forms for some reason.

另一种选择是将base_url添加到所有表单操作中,以便您的应用程序可以在不同的环境中工作,然后在控制器接收到该操作时将其解析出来

Another alternative would be having base_url added to all the form actions so your application can work in different environments, and then having it parsed out when the action is received by the controller.

是否有一种简单的方法来设置变量来完成类似的任务,而Zend已经内置了该变量?
如果没有,那有什么解决方案?

Is there a simple way to set a variable to do something like this, already built into Zend?If not, what's a solution?

我似乎在任何地方都找不到解决这个问题的东西。

I can't seem to find anything addressing this issue anywhere.

推荐答案

问题是由在操作中包含初始斜杠引起的。当设置带有根相对链接的链接时,等效于当目标锚位于相同域名下时设置绝对链接。 ( / form / save等效于 )。

The problem is caused by including the initial slash in the action. When you set a link with a root-relative link, it is equivalent to setting an absolute link, when the destination anchor is under the same domain name. ("/form/save" is equivalent to "http://localhost/form/save").

在您的情况下,如果要将数据发送到其他控制器/操作,听起来好像需要在表单中添加基本URL。您使用的是 Zend_Form (对吗?),最优雅的解决方案是将 Zend_Form 子类化,从而覆盖 setAction()方法在您设置的每个操作前添加基本URL。

In your case, it sounds like you need to add a base URL to the form, if you want to send the data to a different controller/action. You're using Zend_Form (right?), the most elegant solution would be to subclass Zend_Form, overridding the setAction() method to prepend your base URL to every action you set.

class My_Form extends Zend_Form
{
    ...
    public function setAction($action)
    {
        $baseAction = rtrim($this->_getBaseUrl(),'/') . $action;  // Will remove a slash from the end of the base URL, so form actions start with a slash.

        parent::setAction($baseAction);
    }

    private function _getBaseUrl()
    {
        // Retrive your base url from somewhere (config might be a good place).
    }
}

现在,当您从<$ c创建新表单时$ c> My_Form 并调用 My_Form :: setAction(),您将自动包含基本URL。

Now whenever you create a new form from My_Form and call My_Form::setAction() you'll have your base URL automatically included.

希望这会有所帮助。

这篇关于Zend_Form设置操作-在子目录中内置的Zend应用程序可以使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 11:06