从 zend 框架中的视图获取基本路径

发布于 2024-07-14 11:00:54 字数 935 浏览 9 评论 0原文

案例:您正在使用 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 技术交流群。

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

发布评论

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

评论(6

素食主义者 2024-07-21 11:00:54

您可以从前端控制器 Zend_Controller_Front::getInstance()->getBaseUrl(); 获取基本 url。 我将其包装在视图助手中

class My_View_Helper_BaseUrl 
{   
    /**
     *  Get base url
     * 
     * @return string
     */
    public function baseUrl()
    {
        return rtrim(Zend_Controller_Front::getInstance()->getBaseUrl(),'/');
    }

}

所以在 html 标记中你有类似 辅助程序中的尾部斜杠被删除,以便当应用程序不在子文件夹中运行时(在这种情况下,baseUrl 为空),该路径仍然有效。

You can get the base url from the Front Controller Zend_Controller_Front::getInstance()->getBaseUrl();. I wrap that in a view helper

class My_View_Helper_BaseUrl 
{   
    /**
     *  Get base url
     * 
     * @return string
     */
    public function baseUrl()
    {
        return rtrim(Zend_Controller_Front::getInstance()->getBaseUrl(),'/');
    }

}

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

飘然心甜 2024-07-21 11:00:54

如果有人想知道最好的方法并且不想浪费 2 小时搜索 Zend/Google。 有一个视图助手可以做到这一点。

$this->view->serverUrl();

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.

$this->view->serverUrl();
错爱 2024-07-21 11:00:54

如果您想在 layout 文件中添加主机名,请打印此内容并获取您的 HOST 名称:

echo $this->serverUrl().$this->baseUrl()

If do you want to host name in your layout file so print this and get your HOST name:

echo $this->serverUrl().$this->baseUrl()
怎言笑 2024-07-21 11:00:54

这是我的 baseUrl 助手:

class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    public function baseUrl() {
        $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
        $server = $_SERVER['HTTP_HOST'];
        $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '';
        $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';
        return "$protocol://$server$port$path";
    }
}

This is my baseUrl helper:

class Zend_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    public function baseUrl() {
        $protocol = isset($_SERVER['HTTPS']) ? 'https' : 'http';
        $server = $_SERVER['HTTP_HOST'];
        $port = $_SERVER['SERVER_PORT'] != 80 ? ":{$_SERVER['SERVER_PORT']}" : '';
        $path = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/\\') . '/';
        return "$protocol://$server$port$path";
    }
}
怎会甘心 2024-07-21 11:00:54
<?php
/**
 *
 * @package   TaMeR Library
 * @copyright (C) 2010 Dennis T Kaplan
 * @license   GPL {@link http://www.gnu.org/licenses/gpl.html}
 *
 * @author       Dennis T Kaplan
 */

/** @see Zend_View_Helper_Abstract */
require_once 'Zend/View/Helper/Abstract.php';

class TaMeR_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    /**
     * Returns site's base url, or file with base url prepended
     *
     * $file is appended to the base url for simplicity
     *
     * @param  string|null $file
     * @return string
     */

    public function baseUrl($file = NULL) {

        $baseUrl = $domain = $subdomain = $protocol = $host = $port = NULL;

        $host = array_reverse(explode('.', $_SERVER['HTTP_HOST']));
        $domain = $host[1].'.'.$host[0];
        $subdomain = (isset($host[2]) ? $host[2] : 'www');
        if(getenv("HTTPS") == 'on') {
            $protocol = 'https';
            $port     = $_SERVER['SERVER_PORT'] != 443 ? ':'.$_SERVER['SERVER_PORT'] : '';
        }else{
            $protocol = 'http';
            $port     = $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '';
        }

        // Remove trailing slashes
        if(NULL !== $file) {
            $file = '/' . ltrim($file, '/\\');
        }else{
            $file = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/';
        }
        $baseUrl = $protocol.'://'.$subdomain.'.'.$domain.$port.$file;
        return $baseUrl;
    }
}
<?php
/**
 *
 * @package   TaMeR Library
 * @copyright (C) 2010 Dennis T Kaplan
 * @license   GPL {@link http://www.gnu.org/licenses/gpl.html}
 *
 * @author       Dennis T Kaplan
 */

/** @see Zend_View_Helper_Abstract */
require_once 'Zend/View/Helper/Abstract.php';

class TaMeR_View_Helper_BaseUrl extends Zend_View_Helper_Abstract {
    /**
     * Returns site's base url, or file with base url prepended
     *
     * $file is appended to the base url for simplicity
     *
     * @param  string|null $file
     * @return string
     */

    public function baseUrl($file = NULL) {

        $baseUrl = $domain = $subdomain = $protocol = $host = $port = NULL;

        $host = array_reverse(explode('.', $_SERVER['HTTP_HOST']));
        $domain = $host[1].'.'.$host[0];
        $subdomain = (isset($host[2]) ? $host[2] : 'www');
        if(getenv("HTTPS") == 'on') {
            $protocol = 'https';
            $port     = $_SERVER['SERVER_PORT'] != 443 ? ':'.$_SERVER['SERVER_PORT'] : '';
        }else{
            $protocol = 'http';
            $port     = $_SERVER['SERVER_PORT'] != 80 ? ':'.$_SERVER['SERVER_PORT'] : '';
        }

        // Remove trailing slashes
        if(NULL !== $file) {
            $file = '/' . ltrim($file, '/\\');
        }else{
            $file = rtrim(dirname($_SERVER['SCRIPT_NAME']), '/') . '/';
        }
        $baseUrl = $protocol.'://'.$subdomain.'.'.$domain.$port.$file;
        return $baseUrl;
    }
}
可可 2024-07-21 11:00:54

这对我有用:

echo $this->serverUrl() 。 $this->url()

This worked for me:

echo $this->serverUrl() . $this->url()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文