Feature Request #4169
Support for subdomains
| Status: | Feedback | Start date: | 08/01/2011 | |
|---|---|---|---|---|
| Priority: | Normal | Due date: | ||
| Assignee: | - | % Done: | 0% |
|
| Category: | Core | |||
| Target version: | v3.4.0 | |||
| Resolution: | Points: | 3 |
Description
There is no easy way to use subdomains with Kohana's routing system now. Below there is my proposition how this could works.
// You need to set up a full host here
Kohana::init(array(
'base_url' => 'http://host.dev',
));
// Set up routes
Route::set('admin', '(<controller>(/<action>(/<id>)))', array('subdomain' => 'admin'))
->defaults(array(
'directory' => 'admin'
'controller' => 'dashboard',
'subdomain' => 'admin',
));
Route::set('profile', '', array('subdomain' => '\w+'))
->defaults(array(
'controller' => 'users',
));
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'welcome',
));
Now it should works (left - url, right - detected route):
http://host.dev -> default http://admin.host.dev -> admin http://user.host.dev -> profile
And reverse routing:
// http://user.host.dev
Route::url('profile', array('subdomain' => 'user'));
// http://admin.host.dev
Route::url('admin');
// http://host.dev/articles
Route::url('defaults', array('controller' => 'articles'));
Of course you should be able to get a subdomain:
// In a controller
$subdomain = $this->request->param('subdomain');
And maybe set up default subdomains just before first Route::set() in bootstrap:
// These values are used on every route by default Route::$default_subdomains_regex = '(www)?'; Route::$default_subdomain = 'www';
Related issues
History
Updated by Matt Button 10 months ago
- Status changed from New to Feedback
- Points changed from 1 to 3
This seems like a good idea, however we may need a better syntax - using route params feels way too hacky.
Not sure if Sam still intends on incorporating routing requests to different hosts, but this could potentially conflict with that.
Updated by Dmitry T. 10 months ago
Why don't you just set the routes depending on the $_SERVER['HTTP_HOST'] variable?
Updated by Sam de Freyssinet 10 months ago
In my opinion, routes must be completely aware of their host server name. In distributed systems it is vital to be able to quickly route to an external application (Kohana powered or not) in a dynamic but maintainable manner– routes lend themselves nicely to this.
So more than just subdomain support, Route should be able to handle any domain syntax and be able to resolve to it correctly.
Updated by Kiall Mac Innes 10 months ago
Have to agree with sam, this is much more complicated that simply supporting subdomains. Anything done to support this needs to be very well thought out so we don't limit ourselves.
Updated by Teodor Milkov 9 months ago
+1 - that'd be great.
I'm setting routes depending on the $_SERVER['HTTP_HOST'], but it feels hackish and gets in the way of reverse routes...
Updated by Isaiah DeRose-Wilson 5 months ago
- Target version changed from v3.3.0 to v3.4.0
Updated by David Pommer 4 months ago
Some quick thoughts:
Route::base('<protocol>://(<subdomain>\.)<domain>')
->defaults(array(
'protocol' => 'http',
'domain' => 'example.com'
));
// example from above
Route::set('home', '', array('subdomain' => 'www'))
->defaults(array(
'controller' => 'home'
));
Route::set('profile', '', array('subdomain' => '\w+'))
->defaults(array(
'controller' => 'users'
));
// or
Route::set('profile', '<protocol>://(<user>\.)<domain>', array('user' => '\w+'))
->defaults(array(
'controller' => 'users'
));
// auth only over HTTPS
Route::set('auth', '<action>', array('action' => (login|logout)'))
->defaults(array(
'protocol' => 'https',
'controller' => 'auth',
));
// routing to FTP
Route::set('kohana-ftp')
->defaults(array(
'protocol' => 'ftp',
'domain' => 'kohanaframework.org',
));
I'm not sure if the "base regex" should appear in every Route::set.
Updated by David Pommer 4 months ago
Renaming Route::base to Route::setup, so you can do:
Route::set('user/mail')
->base('<protocol>:<user>@<domain>')
->defaults(array(
'protocol' => 'mailto',
));