Search code examples
phpzend-frameworkamazon-s3zend-config

Zend - changing image URL path for deployment


The title might be confusing as I'm not sure myself on how to explain this. I'm sure its a pretty simple solution.

I'm moving all my static images,css,js to S3 - so now they can be accessed via

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 is a CNAME pointing to files.xyz.com.s3.amazonaws.com

Now in my Zend layout and views - I'm accessing these with the full URL egs

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

My concern is when I'm testing on localhost - I dont want the data to be fetched from S3 but from my local hard disk

So I want to do something like this. In my application.ini - I should be able to specify

resources.frontController.imageUrl = http://localhost

And when I'm deploying - simply change that to

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

What is the best approach to handling this Thanks


Solution

  • Create a view helper

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

    In appilication.ini

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

    You can call like this

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

    So you can enable/disable the s3 easily.