What would be a good strategy for a variable to prefix a static resource?

We are scaling up the website, and we would like to plan for the future where we might want to place our images in a subdomain (or, possibly, even in a separate domain, for example CDN ). We are currently referencing images in our HTML / PHP code using the following HTML code:

<img src="/images/ourlogo.jpg" alt="Our Logo" />

I was thinking about starting a company agreement to switch to:

<img src="<?php echo STAT_IMG;?>ourlogo.jpg" alt="Our Logo" />

where STAT_IMG is a global PHP constant that would initially be defined as identical to the current situation, i.e.

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

but can later be changed to something like:

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

Will I encounter any problems doing this?

Things I've already thought about:

  • , , , .
  • ( , PHP HTML ).
  • , https ( -). , , (, gmail) https, , http ( ), (, IE). encosia , STAT_IMG " ", . define('STAT_IMG', '//www.superfastcdn.com/');. , .
    • , . define('STAT_IMGS', 'https://www.example.com/images/'); define('STAT_IMGNS', 'http://www.example.com/images/'); (define('STAT_IMG', '/images/');).
  • , javascript CSS.
+3
1

, - , , Rails, Symfony Django. , - , .

, :

<?

$my_domain = "something.com";

function static_url($relative_path, $SSL=false) {
  $prefix = $SSL ? 'https' : 'http';
  return "{$prefix}://{$my_domain}{$relative_path}";
}

:

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

, https:

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

Source: https://habr.com/ru/post/1787624/


All Articles