Zend - 更改用于部署的映像 URL 路径


Zend - changing image URL path for deployment

标题可能会令人困惑,因为我自己不确定如何解释这一点。我相信这是一个非常简单的解决方案。

我正在将所有静态图像,css,js移动到S3 - 所以现在可以通过

例如:

http://files.xyz.com/images/logo.gifhttp://files.xyz.com/images/submit_button.gifhttp://files.xyz.com/style/style.csshttp://files.xyz.com/js/jquery.js

files.xyz.com 是指向 files.xyz.com.s3.amazonaws.com 的别名记录

现在在我的 Zend 布局和视图中 - 我正在使用完整的 URL 访问它们电子电气化

<img src="http://files.xyz.com/images/logo.gif"/>

我担心的是当我在本地主机上进行测试时 - 我不希望从 S3 获取数据,而是从我的本地硬盘获取数据

所以我想做这样的事情。在我的应用程序中.ini - 我应该能够指定

resources.frontController.imageUrl = http://localhost

当我部署时 - 只需将其更改为

resources.frontController.imageUrl = http://files.xyz.com
并在视图中访问它,例如imageUrl;?>/图像/徽标.gif"/>

处理此问题的最佳方法是什么谢谢

创建视图帮助程序

public function imageUrl()
    {
        $config = Zend_Registry::get('config');
        if($config->s3->enabled){
            return $config->s3->rootPath; 
        }else{
            return $this->view->baseUrl(); 
        }
    }

在申请中.ini

s3.enabled        = 1
s3.rootPath       = https://xxxxx.s3.amazonaws.com

你可以这样打电话

<img src="<?php echo $this->imageUrl();?>/images/logo.gif"/>

因此,您可以轻松启用/禁用s3。

尝试 baseUrl 视图帮助程序。在应用程序中指定 URL.ini如下所示:

[production]
resources.frontController.baseUrl = "http://files.xyz.com"

那么在您的视图中:

<img src="<?php echo $this->baseUrl('images/someimage.jpg'); ?>">

假设您正在设置APPLICATION_ENV并在application/configs/application.ini文件中使用特定于环境的部分,那么您的想法和视图帮助程序想法的组合似乎是要走的路。

application/configs/application.ini

[production]
cdn.baseUrl = "http://files.zyz.com"
[development]
cdn.baseUrl = "http://mylocalvirtualhost/assets/img"

然后是视图帮助程序:

class My_View_Helper_CdnBaseUrl extends Zend_View_Helper_Abstract
{
    protected static $defaultBase = '';
    protected $base;
    public function cdnBaseUrl($file = '')
    {
        return rtrim($this->getBase(), '/') . '/' . ltrim($file, '/');
    }
    public static function setDefaultBase($base)
    {
        self::$defaultBase = $base;
    }
    protected function getBase()
    {
        if (null === $this->base){
            $this->base = self::$defaultBase;
        }
        return $this->base;
    }
}

application/Bootstrap.php

protected function _initCdn()
{
    $options = $this->getOptions();
    My_View_Helper_CdnBaseUrl::setDefaultBase($options['cdn']['baseUrl']);
}

然后,视图脚本中的用法如下:

<img src="<?= $this->cdnBaseUrl('root/relative/path/to/img.jpg') ?>" alt="Some image">

当然,您需要添加autloadernamespaces和视图助手前缀路径以匹配您自己的命名空间等。