从 zend 框架中的视图获取基本路径
案例:您正在使用 Zend Framework 开发一个站点,并且需要部署 webapp 的文件夹的相对链接。即 mysite.com/folder
在线和 localhost:8080
下发展。
无论部署位置如何,以下内容在控制器中都可以很好地工作:
$this->_helper->redirector->gotoSimple($action, $controller, $module, $params);
以及视图脚本内的以下内容,即。 index.phtml:
<a href="<?php echo $this->url(array('controller'=>'index', 'action' => 'index'), null, true); ?>">
但是链接到图像或样式表时如何获得正确的基本路径? (例如,在layout.phtml文件中):
<img src='<?php echo WHAT_TO_TYPE_HERE; ?>images/logo.png' />
和
$this->headLink()->appendStylesheet( WHAT_TO_TYPE_HERE . 'css/default.css');
WHAT_TO_TYPE_HERE
应该替换为
<img src="/folder/images/logo.png />` on mysite.com and `<img src="/images/logo.png />
本地主机上给出的内容
Case: you're developing a site with Zend Framework and need relative links to the folder the webapp is deployed in. I.e. mysite.com/folder
online and localhost:8080
under development.
The following works nice in controllers regardless of deployed location:
$this->_helper->redirector->gotoSimple($action, $controller, $module, $params);
And the following inside a viewscript, ie. index.phtml:
<a href="<?php echo $this->url(array('controller'=>'index', 'action' => 'index'), null, true); ?>">
But how do I get the correct basepath when linking to images or stylesheets? (in a layout.phtml file, for example):
<img src='<?php echo WHAT_TO_TYPE_HERE; ?>images/logo.png' />
and
$this->headLink()->appendStylesheet( WHAT_TO_TYPE_HERE . 'css/default.css');
WHAT_TO_TYPE_HERE
should be replaced with something that gives
<img src="/folder/images/logo.png />` on mysite.com and `<img src="/images/logo.png />
on localhost
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以从前端控制器
Zend_Controller_Front::getInstance()->getBaseUrl();
获取基本 url。 我将其包装在视图助手中所以在 html 标记中你有类似
辅助程序中的尾部斜杠被删除,以便当应用程序不在子文件夹中运行时(在这种情况下,baseUrl 为空),该路径仍然有效。
You can get the base url from the Front Controller
Zend_Controller_Front::getInstance()->getBaseUrl();
. I wrap that in a view helperSo in the html markup you have something like
<img src="<?php echo $this->baseUrl();?>/images/logo.png"/>
The trailing slash is stripped out in the helper so that when the application isn't run in a sub folder (baseUrl is blank in that case) the path will still work.如果有人想知道最好的方法并且不想浪费 2 小时搜索 Zend/Google。 有一个视图助手可以做到这一点。
In case anyone wants to know the best way and doesn't want to waste 2 hours searching Zend/Google. There is a view helper to do that.
如果您想在
layout
文件中添加主机名,请打印此内容并获取您的HOST
名称:If do you want to host name in your
layout
file so print this and get yourHOST
name:这是我的 baseUrl 助手:
This is my baseUrl helper:
这对我有用:
echo $this->serverUrl() 。 $this->url()
This worked for me:
echo $this->serverUrl() . $this->url()