views:

95

answers:

2

Is it possible to do domain-specific routes in cakephp?

for example, let's say I have 2 domains: manufacturer.com and productname.com

productname.com is parked on manufacturer.com.

I'd like to create a route like this:

Router::connect('http://www.productname.com/', array('controller' => 'products', 'action' => 'view', 'productSlug'));

so basically the index for manufacturer.com is the default pages/index but for productname.com it should be products/view/productSlug

Is this possible with Cake?

+1  A: 

This is possible, but I probably wouldn't use routes to do it. Mostly because I'm no wizard when it comes to them ;).

For the kind of flexibility you want, I would think that you could route all requests to a centralized controller (probably your AppController, actually). Then have that controller determine the logic for invoking different controllers and actions.

Travis Leleu
Sorry for the late reply, been busy:)Not bad idea. I'll keep that in mind, though I came up with something else myself(thanks to your answer) :)
Pichan
A: 

I'm just gonna answer my own question with the solution I came up with.

Instead of trying to check the domain inside the routes, a better way would be checking the hostname before even defining routes. For example:

if($_SERVER['HTTP_HOST']=='productname.com')
   Router::connect('/', array('controller' => 'products', 'action' => 'view', 'productSlug'));
else
   Router::connect('/', array('controller' => 'pages', 'action' => 'index'));

Any opinions? Good or bad? I haven't tested it yet, but at least I don't see anything wrong :)

Pichan
I think this should work, nice job solving your own issues! My only concern would be the hardcoding of the hostnames in your codebase. If you did something similar, but in the AppController, you could retrieve host/routing data from a database, which will make it easier to maintain and put an admin interface into.
Travis Leleu
Thanks. If it were a bigger site, I would probably be concerned about hardcoded hostnames too, but in this case it really doesn't matter. I'd like to know more about the method you suggested. Could you perhaps share some example code if it's not too much trouble?
Pichan