开发者

Routing depending on the subdomain

Please help me. I am quite new to kohana. How best to do so the controller was chosen based on the subdomain. For example:

www.site.com -> Controller: siteroot. Method: run admin.site.com -> Controller: adminsite. Method: run moderator.site.com -> Controller: moderatorsite. Method: run director.site.com -> Contro开发者_JS百科ller: directorsite. Method: run default: supervisor: partnersite. Method: run

The run method performs an action for these sub-domains, and will bring a page from the overseers modules.

I use kohana v3.0


I don't think Kohana offers any way to deal with this directly, but you could always add some custom code to your bootstrap.php file that sets up different routes depending on the current subdomain:

switch ($_SERVER['SERVER_NAME'])
{
    case 'site.com':
        // Default routes.
        $controller = 'siteroot';
        break;
    case 'admin.site.com':
        // Admin routes.
        $controller = 'adminsite';
        break;
    // Etc.
}

Route::set('default', '(<controller>(/<action>(/<id>)))')
     ->defaults(array(
                'controller' => $controller,
                'action' => 'run')); 

Do you really need a separate domain for each case though? It might be more sensible just to use site.com/admin, site.com/moderator, etc.


I don't think it will work out of box, MatW. It will be true if it is a subdirectory of the app_path, but if it isn't, it will never be routed to index.php of the folder. It can be done with htaccess or httpd.ini from apache.


As that subdomain will be mapping to a directory anyway there should be no need to add any custom code at all, this is exactly what Routes are for in Kohana 3.

subdomain: admin.site.com

maps to directory: ~/public_html/admin/

controller: Controller_Adminsite

controller directory: ~/application/classes/controller/adminsite.php

the route for bootstrap.php:

Route::set('admin', 'admin(/<action>(/<id>))')
        ->defaults(array(
                'controller' => 'adminsite',
                'action'     => 'run',
        ));


It looks like someone has actually created a submodule for doing subdomain routing:

https://github.com/jeanmask/subdomain

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜