Zend Framework:获取公共路径,获取应用程序 url
是否有一些适当的 Zend 方法用于:
a)接收 /public 目录的路径
b)接收应用程序 url
实际上我正在使用 Controller 中定义的方法,但如果它们退出,使用现成的方法感觉是正确的。
protected function _getApplicationUrl() {
return $_SERVER['SERVER_NAME'];
}
protected function _getPublicPath() {
return realpath(APPLICATION_PATH . '/../public/');
}
Are there some proper Zend methods for:
a) receiving path to /public directory
b) receiving application url
Actually I'm using methods defined in Controller, but it feel right to use ready methods if they exits.
protected function _getApplicationUrl() {
return $_SERVER['SERVER_NAME'];
}
protected function _getPublicPath() {
return realpath(APPLICATION_PATH . '/../public/');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
关于应用程序 URL,
Zend_Controller_Request_Http
有一个getRequestUri()
方法,但它故意(有点令人沮丧)排除了 URL 的方案和主机名部分。在我的应用程序中,我在引导程序中获取$_SERVER['HTTP_HOST']
并将其存储在注册表中,以便稍后在构建完整 URL 时使用它。根据记忆,不,没有任何内置方法可以获取
public
文件夹的位置,但您拥有的代码很好。我见过的大多数应用程序都定义了index.php
中的所有路径,我认为这稍微安全一些(只是因为路径名称设置得更快并且绝对不可变) )并且稍微快一些,但是我们不要陷入关于微优化的争论! :-)Regarding the application URL,
Zend_Controller_Request_Http
has agetRequestUri()
method, but it deliberately (and somewhat frustratingly) excludes the scheme and hostname parts of the URL. In my apps I have resorted to grabbing$_SERVER['HTTP_HOST']
in the bootstrap and storing it in the registry so that I can use it later when constructing full URLs.And from memory, no, there isn't any built-in method to get the location of the
public
folder, but the code you have is fine. Most apps I've seendefine()
all the paths inindex.php
, which I suppose is slightly safer (only because the path names get set sooner and become absolutely immutable) and ever so slightly faster, but lets not get into a debate about micro-optimizations! :-)1) 如果你的虚拟主机指向 ZF /public 那么在 View 中你可以通过辅助方法
$this->baseUrl();
在控制器$this->view-> 中获取路径;baseUrl();
否则创建您自己的助手并使用它。2) 在控制器中
$this->getRequest()->getHttpHost();
1) If your virtual host point to ZF /public then in View you can get path by helper method
$this->baseUrl();
In controller$this->view->baseUrl();
Otherwise create your own helper and use it.2) In controller
$this->getRequest()->getHttpHost();
内置 php-function getcwd()将为您提供站点主机文件夹的路径(例如输出“/home/my_cp/public_html/my_site.loc/www”)。然后,您可以构建任何您想要的路径。
Built-in php-function getcwd() will give you the path to your site-host folder (ex. output "/home/my_cp/public_html/my_site.loc/www"). And then, you can construct whatever path you want.