2012-06-04 37 views
2

标题可能会令人困惑,因为我不确定自己如何解释此问题。我相信它是一个非常简单的解决方案。Zend - 用于部署的图像URL路径更改

我将所有我的静态图片,CSS,JS给S3 - 所以现在他们可以通过

EGS访问:

 
http://files.xyz.com/images/logo.gif 
http://files.xyz.com/images/submit_button.gif 
http://files.xyz.com/style/style.css 
http://files.xyz.com/js/jquery.js 

files.xyz.com是一个指向文件CNAME .xyz.com.s3.amazonaws.com

现在在我的Zend的布局和意见 - 我与完整的URL访问这些 EGS

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

我担心的是,当我在本地主机上测试 - 我不希望将数据从S3但是从我的本地硬盘

所以我想要做这样的事情来获取。在我的application.ini - 我应该能够指定

 
resources.frontController.imageUrl = http://localhost 

,当我部署 - 简单的改变,要

 
resources.frontController.imageUrl = http://files.xyz.com 
 
And access it in the view like 
<img src="<?php echo $this->imageUrl;?>/images/logo.gif"/> 

什么是处理这个 感谢的最好方法

回答

3

创建一个视图助手

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

在appilication.ini

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

你可以这样打电话

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

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

+0

谢谢 - 这正是我一直在寻找的。 – Gublooo

+0

欢迎您:) – Venu

0

尝试baseUrl view helper。指定像这样在你的application.ini的网址:

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

然后在您的观点:

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

感谢尤金。我想过,但不是baseUrl设置为 - 在我的示例http://xyz.com - 所以如果我将其更改为http://files.xyz.com - 它会影响其他地方的预期http://xyz.com right – Gublooo

+0

老实说,我不确定这是否会影响由url视图助手生成的URL,例如'$ this-> view-url(array('index','index'))''。如果它导致一个问题,你可能想要定义你自己的视图助手,它的功能和baseUrl助手的功能相似。 –

0

假设您正在设置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']); 
} 

Thenm用法如下:

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

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