Kohana PHP. One application, many domains?

by Stii

In Kohana PHP‘s config file they have a bunch of weird settings that you have to specify. I cannot for the life of Brian understand why! :P

I’m talking about this. The very first thing you need to set:

<?php

$config['site_domain'] = 'mysite.com';

Now, I can understand the logic if you want to run your site off a directory in your domain like mysite.com/mysite, but still. Personally, I think, the URL should not be forced and it should rather just use $_SERVER['SERVER_NAME'] to determine the site domain. What they should ask is for a directory config item if you want to run your site off a directory on your domain:

<?php
//Leave blank if site is in the root directory
$config['site_directory'] = 'mysite';

Now lets suppose you want to do a mobile site. You should never have to replicate your code and maintain 2 code bases. You know. The DRY principles? I want to run two domains off the same code base, but make my decision in the code whether I want to display default site templates or whether I must display the mobile templates. (See: Overriding default template in Kohana PHP if you’re unsure what I’m on about). For example: I have my normal site running on http://mysite.com and I want my mobile site to live at http://mysite.mobi. The difference between the two are the templates. I want to use the rest of the codebase as is since they’re all the same for both sites.

The solution is pretty much straight forward. All you need to do is:

<?php

$config['site_domain'] = $_SERVER['SERVER_NAME'];

or (if your application lives in a directory)

<?php

$config['site_domain'] = $_SERVER['SERVER_NAME'] . '/mysite';

and viola! It works like a charm. But it is still very unnecessary! That’s my beef about it. This is one of those weird things that spilled over from Code Igniter. Maybe this could change at some stage? Maybe I’m missing something and in fact it is quite critical that you do specify it like they prefer? Hope someone could clarify. It is quite possible that I’m missing something very obvious… I’m known for that from time to time!

UPDATE… Sigh

It turns out that if you leave $config['site_domain'] blank, it defaults to using $_SERVER['SERVER_NAME'].

<?php

$config['site_domain'] = '';

At least, now I know!

Also, read Darb‘s comment below on why you’d want to enforce a single domain. Makes sense!