PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, April 22, 2022

[FIXED] how to set the full base url to a cdn while using HtmlHelper in cakephp?

 April 22, 2022     cakephp, cakephp-2.0, cakephp-2.3, cdn     No comments   

Issue

My cakephp app is running on 2.4.0

It is already running live on yourapp.com

I am attempting to use Amazon CloudFront to serve static assets like css, js, and images.

The CDN domain I chose was cdn.yourapp.com

Sadly, when I tried to use it this way:

echo $this->Html->css('alpha_landing/styles', array('fullBase' => $cdnBaseUrl));

where $cdnBaseUrl is http://cdn.yourapp.com/

I did not get back the correct url I was expecting.

I was expecting

http://cdn.yourapp.com/css/some.css

But I got back

http://yourapp.com/css/some.css

How can I overcome this problem?


Solution

Two solutions:

Simply write a HtmlHelper that can override the default image, css, etc functions

See https://stackoverflow.com/a/9601207/80353 for details

or

you can rewrite the assetUrl function in your AppHelper so that you need not rewrite all the related functions.

public function assetUrl($path, $options = array()) {
    $cdnBaseUrl = Configure::read('App.assetsUrl');
    $legitCDN = (strpos($cdnBaseUrl, '://') !== false);
    if (is_array($path)) {
        $path = $this->url($path, !empty($options['fullBase']));
        if ($legitCDN) {
            return rtrim($cdnBaseUrl, '/') . '/' . ltrim($path, '/');
        }
        return $path;
    }
    if (strpos($path, '://') !== false) {
        return $path;
    }
    if (!array_key_exists('plugin', $options) || $options['plugin'] !== false) {
        list($plugin, $path) = $this->_View->pluginSplit($path, false);
    }
    if (!empty($options['pathPrefix']) && $path[0] !== '/') {
        $path = $options['pathPrefix'] . $path;
    }
    if (
        !empty($options['ext']) &&
        strpos($path, '?') === false &&
        substr($path, -strlen($options['ext'])) !== $options['ext']
    ) {
        $path .= $options['ext'];
    }
    if (isset($plugin)) {
        $path = Inflector::underscore($plugin) . '/' . $path;
    }

    $path = $this->_encodeUrl($this->assetTimestamp($this->webroot($path)));
    if ($legitCDN) {
        $path = rtrim($cdnBaseUrl, '/') . '/' . ltrim($path, '/');
    }
    return $path;
}

This is the sample code for the assetUrl

props to @lorenzo at https://github.com/cakephp/cakephp/issues/2149 for this solution

P.S.: I have rewritten the above as a Plugin.

So you can simply have the AppHelper extend this CDNAppHelper instead.

https://github.com/simkimsia/UtilityHelpers



Answered By - Kim Stacks
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing