php Zend Framework $this->baseUrl() 总是返回当前页面 :( 为什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1332787/
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
Zend Framework $this->baseUrl() always returns the current page :( why
提问by Ali
check out this website www.fltdata.com. For some reason the home link no matter what page you go on instead of pointing to the home page it points to the current page. It works fine on my localhost but online its behaving like this. The href value of the home link is just : $this->baseUrl()
查看这个网站 www.fltdata.com。出于某种原因,无论您访问哪个页面,主页链接都指向当前页面,而不是指向主页。它在我的本地主机上运行良好,但在网上它的行为是这样的。主页链接的 href 值只是: $this->baseUrl()
Whats wrong here..
这里有什么问题..
=== EDIT===
=== 编辑===
Well I have created a helper which is as below:
好吧,我创建了一个助手,如下所示:
class Zend_View_Helper_BaseUrl
{
protected $_baseUrl;
function __construct()
{
$fc = Zend_Controller_Front::getInstance();
$this->_baseUrl = $fc->getBaseUrl();
}
function baseUrl()
{
return $this->_baseUrl;
}
}
ANd this is I guess whats being called whenever I call $this->baseUrl. Could this be the problem - however it works okay in my localhost and not online so - must be some kind of configuration issue - where should I look?
这是我猜每当我调用 $this->baseUrl 时会调用什么。这可能是问题 - 但是它在我的本地主机上工作正常而不是在线所以 - 一定是某种配置问题 - 我应该在哪里看?
回答by Stefan Gehrig
It's hard to tell what's going wrong here without knowing more details about your ZF setup and e.h. the url-rewriting-setup. The logic behind Zend_Controller_Request_Http::getBaseUrl()- this is where your call to Zend_View_Helper_BaseUrl::baseUrl()will beproxied to if no baseUrlis set explicitly in the front-controller - is quite complex and involves 4 different server variables (and some more in the process of determining the current request URI).
如果不了解有关 ZF 设置的更多详细信息以及 url-rewriting-setup,则很难判断这里出了什么问题。背后的逻辑Zend_Controller_Request_Http::getBaseUrl()-Zend_View_Helper_BaseUrl::baseUrl()如果baseUrl在前端控制器中显式设置no,则您的调用将被代理到该逻辑-非常复杂,涉及 4 个不同的服务器变量(在确定当前请求 URI 的过程中还有更多)。
The easiest thing to overcome this problem would perhaps be to set the baseUrl(relative to the server root) on the front-controller in your bootstrap. If you're using the new Zend_Application-bootstrapping you'll just have to add
克服这个问题的最简单的方法可能是baseUrl在引导程序中的前端控制器上设置(相对于服务器根目录)。如果你使用新的Zend_Application-bootstrapping 你只需要添加
resources.frontController.baseUrl = "/subdir"
to your configuration.
到您的配置。
EDIT
编辑
Your helper more or less resembles the original baseUrl-helper shipped with the ZF. The original one also proxies to Zend_Controller_Front::getBaseUrl(). Which version of ZF do you use? Is there a reason why you don't use the shipped baseUrl-helper?
您的助手或多或少类似于baseUrlZF 附带的原始-helper。原来的也代理到Zend_Controller_Front::getBaseUrl(). 您使用哪个版本的 ZF?您是否有理由不使用随附的baseUrl-helper?
Have you tried to set the baseUrlin your bootstrapping explicitly?
您是否尝试过baseUrl在引导程序中明确设置?
回答by Stefan Gehrig
I usually use a configuration object for base url and then define it in the index.php in public/index.php
我通常使用基本 url 的配置对象,然后在 public/index.php 的 index.php 中定义它
$config = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV);
$baseUrl = $config->baseHttp;
define('BASE_URL', $baseUrl);`
in the configs/application.ini I have this under production section
在 configs/application.ini 我在生产部分有这个
[production]
baseHttp = "http://whatever.net"
This way if I ever need to use a different base url in developement I could just add a diiferent variable for baseHttp personally I think if you want a the baseurl not to get convoluted it is a simpler approach than wrapping in its own class. Sometimes OOP is too much OOP. Plus it would make an installation program more easier to manage. You could even get the $_SERVER variable to get the hostname and wrap it with http://
这样,如果我需要在开发中使用不同的基本 url,我可以个人为 baseHttp 添加一个不同的变量,我认为如果您希望 baseurl 不会变得复杂,那么它是比包装在自己的类中更简单的方法。有时 OOP 太多了。此外,它还将使安装程序更易于管理。您甚至可以获取 $_SERVER 变量来获取主机名并用 http:// 包装它
回答by Floyd
Zend Framework is always returning the current page because you are not using a trailing slash after calling $this->baseUrl();.
Zend Framework 总是返回当前页面,因为在调用$this->baseUrl();后没有使用尾部斜杠;.
To resolve the issue append a trailing slash after calling $this->baseUrl();in your view like so:
要解决此问题,请在调用$this->baseUrl();后附加一个斜杠;在你看来像这样:
The Correct Way
正确的方法
<a href="<?php echo $this->baseUrl() . "/"; ?>">Home Link</a>
or
或者
<a href="<?php echo $this->baseUrl(); ?>/">Home Link</a>
Omitting the trailing slash results in a link to the page you're currently viewing.
省略尾部斜杠会生成指向您当前正在查看的页面的链接。
The Incorrect Way
不正确的方式
<a href="<?php echo $this->baseUrl(); ?>">Home Link</a>
回答by Arianne
I just had a similar problem when using $this->baseUrl in my view. I found using this would give me the url of the current page:
我在使用 $this->baseUrl 时遇到了类似的问题。我发现使用它会给我当前页面的网址:
baseUrl; ?>"> http://www.yourwebaddress.com/current-page
基本网址;?>"> http://www.yourwebaddress.com/current-page
and this would give me the real baseurl regardless of the current page (note the slash added to the end of the url)
无论当前页面如何,这都会给我真正的 baseurl(注意添加到 url 末尾的斜杠)
baseUrl; ?>/"> http://www.yourwebaddress.com/
基本网址;?>/"> http://www.yourwebaddress.com/

