I want to use in my project the ability to easily change the image repository. That is, while I have them on the main server in a separate folder, but later, I may want to transfer them to a separate subdomain. In addition, there will be a general move (change of address). That is, it is impossible to prescribe in the templates a hard-coded full path along with the server address.
I correctly understand that the best solution is to have a variable in the system, obtained from the configuration containing the desired address, and prescribe all the paths with it in the templates?
2 answers
The option is to distribute pictures from a subdomain, but don’t touch the folder structure in it.
Today, pics.supersite.tld
points to the same place as the site itself, buy Amazon S3 tomorrow, and there repeat the folder structure with pictures 1: 1, and flip the pics
subdomain making it CNAME kartinki-1.supersite.tld.amazonaws.com
; the day after tomorrow buy a dedicated server, put nginx
on it and transfer the domain to it.
No shamanism with variables, patterns, dynamics. DNS only.
The question you got rhetorical. It's obvious that if the “container” with images lives its own life, wandering across the Internet, it is reasonable to change the path to it in one configuration file that will be connected on all pages and issue a variable or constant with this information on all pages of the project .
- Well, it's not that you wander straight all the time, but it will change. And to change links everywhere is unreal. I, sobsno, this method and described, but I wanted to verify the loyalty. But one thing: it happens that the paths are written in JS-scripts (for uploading pictures via JS), as in this case? Something like: <div style = 'display: none' id = 'img' data-url = '<? = IMG?>'> </ Div>, where IMG is a constant from the configuration? And JS will take value from here. That is, as in the classic transfer of values ​​from PHP to JS. - Oleg Arkhipov
- Upload the config file for Js scripts generated by the php script /*JSConf.php*/ var SITE_URL = <? = $ Conf ["STATIC_URL"]?>; In HTML: <script src = "JSConf.php"> </ script> - Fucking Babay
- one@ Yoharny Babay, why make an extra request? It is better to set all the necessary variables directly in the template in the head. <script> var STATIC_URL = '<? = $ conf ["STATIC_URL"]?>'; </ script> - Ilya Pirogov
- Yes, better like that. - Fucking Babay
- So what can you do with CSS? - Oleg Arkhipov
STATIC_URL
andMEDIA_URL
, which are set in the configuration file, and then used in templates - neoascetic