Tuesday, March 1, 2011

how to incorporate subdomains into MVC framework

Over the time i developed my own framework and it's working great - it's light, it's fast and is proven that can handle decent load.

Now I'm working on project that can be described as a online shop where every user will have his sub domain where his items will be available for sale.

My framework works by splitting request url by / and determening what is the controller, actions params.. etc..

and that works just fine, but what to do with subdomains?

i've modified my reqest object, so when i type lets say:

http://kodi.shop.local/

(i added SeverAlias *.shop.local )

this is how my request object look like:

DPLS_Request Object
(
    [_request] => Array
        (
            [0] => 
            [1] => 
            [action] => defaultAction
            [controller] => home
        )

    [_params] => Array
        (
        )

    [_rurl:private] => kodi.shop.local
    [_segment] => kodi
    [get_params] => Array
        (
        )

)

so _segment is the subdomain, and later i can use it in code to validate it vs username and other stuff but i'm having conceptual problems even before that. my problem is that my framework expect some controller and actions to be passed, and because all he gets at the end of URL is / he assumes that it shoud display index page :(

so what to do next ... how to incorporate subdomains in whole mvc controllers/actions story?

one quick and dirty sollution is to modify part of my request class:

if($this->_request['controller']==''){ 
    $this->_request['controller'] = DEFAULT_CONTROLLER; 
}
if($this->_request['action']==''){
    $this->_request['action'] = DEFAULT_ACTION; 
}

and to wrap that in yet another if to test if _segment is present, and if it is then assign to controller DEFAULT _SHOP _CONTROLLER, which i will define in config file as lets say 'store'

so the request above will be analog to typing http://shop.local/store/ (it will run 'store' controller and default action)

what would you do in this case? is there any "best practice" when dealing with subdomains and controllers and actions?

From stackoverflow
  • What you're talking about sounds like multi-tenancy. Mike Hadlow does a good write-up of his approach to the issue in an ASP.NET MVC app he's building which you can read here: http://mikehadlow.blogspot.com/2008/11/multi-tenancy-part-2-components-and.html

    Giraffe : Hmm, just noticed you're building your own framework, in which case you would probably need to supply the features this approach relies on. Sorry, thought you were using one of the downloadable frameworks which would probably have had similar extension points to those MH is using.
  • @Giraffe, thanks for your help, but as you say in your comment to your comment i'm using my own framework.

    I already modified code, and it works - i just want to know is that right way to do it.

    Any other ideas?

  • It sounds like, to me, the approach you are describing is the logical next step to implement the features you are adding. The new features will bring some added complexity but your Request object/Router should do a good job of encapsulating the logic of "What controller do I invoke when a segment is provided? When a segment AND a controller are provided?" etc. I say go for it.

  • I personally wouldn't start adding all kinds of 'specific' conditions into your framework related to subdomains, because what subdomains means in one situation doesn't necessarily mean the same thing in another situation. (Besides, the more the framework assumes, the crappier it gets)

    For example, some people uses subdomains to define locale (en.site.com for english version, fr.site.com for french...), yet with this set up, it would use the 'same' controllers no matter what language the site is translated to.

    Just do a simple rewrite rule like: rewrite *.store.site.com into *.site.com/store/

0 comments:

Post a Comment