Serving static content from subdomain? |
[eluser]gwerner[/eluser]
I'm in the process of splitting resources across domains. I've read a few different ways on how to handle this. From what I've read the correct way to handle is to use absolute paths versus relative. If this is true, how would I handle this as I push my local development live? The domains won't match from local to live. The base_url allows for a single domain only as far as I've read. Should I create an asset path helper and autoload that? Or, is there something I'm missing here? http://www.example.com would operate normally. http://images.example.com/assets would point to my asset folder that exists in the root.
[eluser]gwerner[/eluser]
After further searching I've found two methods for handling absolute paths. Anyone care to chime in if either of these is worth executing? Option 1 Define a constant in the config/constants Code: define('ASSETS_URL', 'http://images.example.com/assets/'); and then access like so Code: <img src="'.ASSETS_URL.'image1.png" /> Option 2 Create a helper file that will do essentially the same thing as the base_url() function. Code: // application/helpers/path_helper.php Then, in either our config.php or in a new configuration file we would add: Code: // config.php Add the path_helper and the configuration file (if you're not using config.php) to the autoload.php file. Code: $autoload['helper'] = array('form', 'url', 'path'); and then access like so Code: <img src="<?=asset_url()?>images/profiles/myface.jpg" alt="" /> I haven't tested either of these. I was hoping to get some thoughts on this before proceeding.
[eluser]PhilTem[/eluser]
Of your options, both will work, the first one may sometimes cause problems because of file scopes and undefined constants, therefore it would be better to use the second option - slightly modified though, because: You better not want to use Code: return base_url() . $CI->config->item('asset_path'); as this will always add your base url to the URI. Better do something like this as a helper Code: function asset_url($path = '') { and in config.php Code: $config['asset_url'] = "http://assets.example.com/"; The fist config value should result in something like Code: echo asset_url('path/to/image.file'); and the second one Code: echo asset_url('path/to/image.file'); You should, of course, make 'asset_url()' more robust in terms to check if there is a http(s) preceding the argument or not so you can still use code like Code: echo asset_url('http://code.jquery.com/jquery.min.js'); without screwing your page PS: You can use arguments for both base_url() and site_url() which will avoid having too many slashes or getting a wrong URL. I never understood why people don't do that
[eluser]gwerner[/eluser]
Thanks for the reply. I want to make sure I'm not misunderstanding something, but It seems your example adds the base url to the URI? Code: $config['base_url'] = 'http://example.com/'; Code: // returns http://example.com/http://assets.example.com/assets/path/to/image.file If I remove base_url from the return I get the results I'm looking for. Code: // returns http://assets.example.com/assets/path/to/image.file Am I misunderstanding something in your example? Thanks again for the help.
[eluser]CroNiX[/eluser]
Code: switch(ENVIRONMENT) //set in index.php Then just extend the url_helper and create a function to get the asset_url Code: if ( ! function_exists('asset_url'))
[eluser]PhilTem[/eluser]
[quote author="gwerner" date="1353535851"]If I remove base_url from the return I get the results I'm looking for. Code: // returns http://assets.example.com/assets/path/to/image.file Am I misunderstanding something in your example? Thanks again for the help. [/quote] You're right, I did not intend to put the base_url() inside the asset_url()-helper. I shouldn't be doing multiple things at the same, it just ends up in doing both with less amount of attention
[eluser]gwerner[/eluser]
Thanks to both of you for the help and insight. Makes sense, and helps perfectly.
[eluser]gwerner[/eluser]
Actually, after messing around with CroNiX's concept a little more, it doesn't seem to work? I like this version because it will give me the option to have multiple development environments set in the config switch. I currently have three development phases, so this will work nicely if I can figure out what is wrong. I keep receiving the error message call to undefined method CI_Config::asset_url() My thought is there is something else that I will need to be add to a CI system file? Maybe by extending. Or, is it something else? This is how I've set up MY_url_helper which is located in the application/helpers directory. Code: if ( ! function_exists('asset_url'))
[eluser]gwerner[/eluser]
So, there are two ways to get this to work that I can see. One is to edit the system Config.php file by adding a method to handle the new asset_url function. Which is probably not the best idea since it is never recommended by anyone. The other is to use PhilTem's return to fetch the config item in place of CroNix's. Code: return $CI->config->item('asset_url') . ltrim($path, '/');
[eluser]CroNiX[/eluser]
Yeah, sorry. That should be Code: return $CI->config->config['asset_url'] . $uri; I quickly copied the base_url helper and forgot that that helper is accessing a specific method in the config CLASS that is only for base_url. |
Welcome Guest, Not a member yet? Register Sign In |