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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/635703/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:20:05  来源:igfitidea点击:

Getting basepath from view in zend framework

phpzend-frameworkzend-viewzend-layout

提问by Mads Mob?k

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/folderonline and localhost:8080under development.

案例:您正在使用 Zend Framework 开发一个站点,并且需要指向部署 webapp 的文件夹的相关链接。即mysite.com/folder在线和localhost:8080正在开发中。

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:

下面是一个视图脚本,即。索引.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):

但是在链接到图像或样式表时如何获得正确的基本路径?(例如在 layout.phtml 文件中):

<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_HEREshould be replaced with something that gives

WHAT_TO_TYPE_HERE应该用可以提供的东西代替

<img src="/folder/images/logo.png />` on mysite.com and `<img src="/images/logo.png />

on localhost

在本地主机上

回答by Akeem

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

您可以从 Front Controller 获取基本 url Zend_Controller_Front::getInstance()->getBaseUrl();。我把它包装在一个视图助手中

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.

因此,在 html 标记中,您可以<img src="<?php echo $this->baseUrl();?>/images/logo.png"/>在帮助程序中删除尾部斜杠之类的内容,以便当应用程序不在子文件夹中运行时(在这种情况下 baseUrl 为空),路径仍然有效。

回答by puppybits

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.

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

$this->view->serverUrl();

回答by irfan khan

If do you want to host name in your layoutfile so print this and get your HOSTname:

如果您想在layout文件中包含主机名,请打印此文件并获取您的HOST姓名:

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

回答by irfan khan

This is my baseUrlhelper:

这是我的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";
    }
}

回答by RoboTamer

<?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;
    }
}

回答by Strategist

This worked for me:

这对我有用:

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

回声 $this->serverUrl() 。$this->url()