CodeIgniter's Subdomain Support

#1
[eluser]Jamie Rumbelow[/eluser]
I'm interested in hearing your experiences with regards to subdomain support for CodeIgniter applications. Has anyone extended the routing mechanism to allow for subdomains... for instance using a different database for each different subdomain?

I'll start to play around with the code and see what I can do with this but I was interested in what the community's done and some of the experiences and problems you've had with implementing a subdomain-based system.

Jamie

#2
[eluser]ELRafael[/eluser]
[quote author="Jamie Rumbelow" date="1257550760"]I'm interested in hearing your experiences with regards to subdomain support for CodeIgniter applications. Has anyone extended the routing mechanism to allow for subdomains... for instance using a different database for each different subdomain?

I'll start to play around with the code and see what I can do with this but I was interested in what the community's done and some of the experiences and problems you've had with implementing a subdomain-based system.

Jamie[/quote]

I tried once and I don't do this anymore. Too much pain for almost nothing.
Code:
$config['base_url'] = 'DYNAMIC' //<-- here's the pain

Now we use a sub directory. And the controllers are separeted in sub directories too!

But if you'll use CI only in the subdomains, no problems.

#3
[eluser]Colin Williams[/eluser]
For starters, I never include a domain in my base_url setting. I don't know why anyone would.

Code:
$config['base_url'] = '/';

And for database configuration, I like to name the database groups by either the server IP or hostname. Then you can set $active_group to something like $_SERVER['SERVER_ADDR'] or $_SERVER['HOSTNAME'], so it's dynamic in that way.

#4
[eluser]Jamie Rumbelow[/eluser]
[quote author="ziggyz" date="1257678916"][quote author="Colin Williams" date="1257671252"]For starters, I never include a domain in my base_url setting. I don't know why anyone would.

Code:
$config['base_url'] = '/';

And for database configuration, I like to name the database groups by either the server IP or hostname. Then you can set $active_group to something like $_SERVER['SERVER_ADDR'] or $_SERVER['HOSTNAME'], so it's dynamic in that way.[/quote]

Ingenious! Why haven't I thought of that![/quote]

Actually, a solution I've come up with - not quite as simple as Colin's, but it works - actually guesses the URL correctly based on server variables. This means you can link with URLs, but it's clever enough to guess it and change it dynamically. [url="http://haughin.com"]Haughin[/url] helped me with this one, so props to him.

Code:
$url = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$url .= "://".$_SERVER['HTTP_HOST'];
$url .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

$config['base_url']    = $url;

I've tested it on a few servers and it hasn't failed for me yet... try it out and let me know if it works for you.

Jamie

#5
[eluser]moodh[/eluser]
I use a standard installation and get subdomains mapped to various controllers without a problem at all:
Index.php example:
Code:
&lt;?php
// Normal app/system paths

// Host address
$host = $_SERVER['HTTP_HOST'];

// Extract eventual subdomain
$subdomain = substr($host, 0, strrpos(substr($host, 0, strrpos($host, '.')), '.'));

// Define subdomain
if($subdomain !== 'www' && $subdomain !== '') {
    define('SUBDOMAIN', $subdomain);
} else {
    define('SUBDOMAIN', FALSE);
}

// Rest of index.php
?&gt;

Routes.php example:
Code:
&lt;?php
if(SUBDOMAIN === FALSE) {
    $route['default_controller'] = "foo";
} else {
    $route['default_controller'] = "bar";
    $route['what-is-my-subdomain'] = 'this_is_my_sub_domain/' . SUBDOMAIN;
}
?&gt;

Edit: Of course this needs a .htaccess that redirects everything, subdomains or not to the root url without changing it.
Edit2: and you dont need to use the constant SUBDOMAIN, another look at $_SERVER['HTTP_HOST']; still contains the subdomain for usage in your code =)


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.