Welcome Guest, Not a member yet? Register   Sign In
Serving static content from subdomain?
#1

[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.
#2

[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
if (!function_exists('asset_url'))
{  
    function asset_url()
    {
        // the helper function doesn't have access to $this, so we need to get a reference to the
        // CodeIgniter instance.  We'll store that reference as $CI and use it instead of $this
        $CI =& get_instance();

        // return the asset_url
        return base_url() . $CI->config->item('asset_path');
    }
}

Then, in either our config.php or in a new configuration file we would add:

Code:
// config.php
$config['asset_path'] = 'root/assets/';

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="&lt;?=asset_url()?&gt;images/profiles/myface.jpg" alt="" />

I haven't tested either of these. I was hoping to get some thoughts on this before proceeding.
#3

[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 = '') {
  static $CI;
  
  isset($CI) OR $CI =& get_instance();
  
  return base_url($CI->config->item('asset_url') . ltrim($path, '/'));
}

and in config.php

Code:
$config['asset_url'] = "http://assets.example.com/";
// OR
$config['asset_url'] = "assets/";

The fist config value should result in something like

Code:
echo asset_url('path/to/image.file');
// http://assets.example.com/path/to/image.file

and the second one

Code:
echo asset_url('path/to/image.file');
// http://www.example.com/assets/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 Wink

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 Tongue
#4

[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/';
$config['asset_url'] = 'http://assets.example.loc/';

Code:
// returns http://example.com/http://assets.example.com/assets/path/to/image.file
return base_url($CI->config->item('asset_url') . ltrim($path, '/'));

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
return $CI->config->item('asset_url') . ltrim($path, '/');

Am I misunderstanding something in your example? Thanks again for the help.
#5

[eluser]CroNiX[/eluser]
Code:
switch(ENVIRONMENT)  //set in index.php
{
  case 'development':
    $config['base_url'] = 'http://localhost/';
    $config['asset_url'] = 'http://localhost/assets/';
    break;
  case 'production':
    $config['base_url'] = 'http://yoursite.com/';
    $config['asset_url'] = 'http://assets.yoursite.com/';
    break;
}

Then just extend the url_helper and create a function to get the asset_url
Code:
if ( ! function_exists('asset_url'))
{
function asset_url($uri = '')
{
  $CI =& get_instance();
  return $CI->config->asset_url($uri);
}
}
#6

[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
return $CI->config->item('asset_url') . ltrim($path, '/');

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 Tongue
#7

[eluser]gwerner[/eluser]
Thanks to both of you for the help and insight. Makes sense, and helps perfectly.
#8

[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'))
{
function asset_url($uri = '')
{
  $CI =& get_instance();
  return $CI->config->asset_url($uri);
}
}
#9

[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, '/');
#10

[eluser]CroNiX[/eluser]
Yeah, sorry. That should be
Code:
return $CI->config->config['asset_url'] . $uri;
in the /application/helpers/MY_url_helper.php

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.





Theme © iAndrew 2016 - Forum software by © MyBB