php - What would be a good strategy for a variable for prefixing a static resource? -


we scaling website , plan future may want host our images in sub-domain (or perhaps separate domain altogether, e.g. cdn). reference images in our html/php code using following html:

<img src="/images/ourlogo.jpg" alt="our logo" /> 

i thinking of starting company convention move to:

<img src="<?php echo stat_img;?>ourlogo.jpg" alt="our logo" /> 

where stat_img global php constant, defined identical current situation, i.e.

define('stat_img', '/images/'); 

but later changed such as:

define('stat_img', 'http://www.superfastcdn.com/'); 

will run issues doing this?

things have thought about:

  • i can see there'll many more string appends in code base - don't expect it'll noticeable in terms of performance.
  • it makes code uglier (especially in example php , html have been mixed).
  • one issue need explicitly use https images (or vice version). example, if put images in email, many clients (e.g. gmail) use https protocol, resources referencing http (i.e. unencrypted protocol) generate mixed content warning in browsers (e.g. ie). this article encosia has idea working around defining stat_img "protocol-less", e.g. define('stat_img', '//www.superfastcdn.com/');. hope idea works.
    • we may need few other constants explicitly define protocol e.g. define('stat_imgs', 'https://www.example.com/images/'); , define('stat_imgns', 'http://www.example.com/images/'); in addition previous non-absolute version (define('stat_img', '/images/');).
  • i need apply same strategy other static resources such javascript , css stylesheets.

it sounds need function -- that's how tends handled in frameworks rails, symfony, , django. in general, encapsulating logic idea, don't find having update more place given design change.

for starters, put in location common across templates:

<?  $my_domain = "something.com";  function static_url($relative_path, $ssl=false) {   $prefix = $ssl ? 'https' : 'http';   return "{$prefix}://{$my_domain}{$relative_path}"; } 

then, put template:

<img src="<?=static_url('images/ourlogo.jpg'); ?>" /> 

or, if need https:

<img src="<?=static_url('images/ourlogo.jpg', true); ?>" /> 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -