【问题标题】:Zend - changing image URL path for deploymentZend - 更改部署的图像 URL 路径
【发布时间】:2012-06-08 13:15:33
【问题描述】:

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

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

例如:

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 是指向 files.xyz.com.s3.amazonaws.com 的 CNAME

现在在我的 Zend 布局和视图中 - 我正在使用完整的 URL 访问这些 例如

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

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

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

resources.frontController.imageUrl = http://localhost

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

resources.frontController.imageUrl = http://files.xyz.com 并在视图中访问它 imageUrl;?>/images/logo.gif"/>

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

【问题讨论】:

    标签: php zend-framework amazon-s3 zend-config


    【解决方案1】:

    创建视图助手

    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。

    【讨论】:

    • 谢谢 - 这正是我想要的。
    【解决方案2】:

    试试baseUrl view helper。在 application.ini 中指定 URL,如下所示:

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

    那么在你看来:

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

    【讨论】:

    • 谢谢尤金。我考虑过这一点,但 baseUrl 没有设置为 - 在我的示例中为 xyz.com - 所以如果我将其更改为 files.xyz.com - 它会在预期 xyz.com 的其他地方生效
    • 老实说,我不确定这是否会影响由 url 查看帮助程序生成的 URL,例如 $this-&gt;view-url(array('index', 'index'))。如果它导致问题,您可能只想定义自己的视图帮助器,其功能类似于 baseUrl 帮助器。
    【解决方案3】:

    假设您正在设置 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 和 view-helper 前缀路径以匹配您自己的命名空间等。

    【讨论】:

      猜你喜欢
      • 2021-11-28
      • 2012-08-17
      • 1970-01-01
      • 2011-06-11
      • 2019-04-07
      • 1970-01-01
      • 2021-10-14
      • 2014-02-28
      • 2020-06-12
      相关资源
      最近更新 更多