Zend - changing image URL path for deployment

1.4k views Asked by At

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

3

There are 3 answers

0
Venu On BEST ANSWER

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.

2
Eugene M On

Try the baseUrl view helper. Specify the URL in your application.ini like this:

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

Then in your views:

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

Assuming you are setting your APPLICATION_ENV and using environment-specific sections in your application/configs/application.ini file, then a combination of your idea and the view helper idea seems the way to go.

In application/configs/application.ini:

[production]

cdn.baseUrl = "http://files.zyz.com"

[development]

cdn.baseUrl = "http://mylocalvirtualhost/assets/img"

Then a view helper:

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

In application/Bootstrap.php:

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

Thenm usage in a view-script is as follows:

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

Of course, you will need to add autloadernamespaces and view-helper prefix paths to match your own namespacing, etc.