Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to route multi subdomain with zend router hostname

I need to create routing in Zend to simply copy the current live site url structure which is sadly inconsistent

What i want to do is to route subdomain as follow:

www.site.com -> static router

a.site.com & b.site.com -> category controller

c.site.com & d.site.com -> location controller

the rest sub domain -> user controller

could anyone guide me how to solve this, thanks.

UPDATE:

First thanks Fge, vote your answer, it works but i need some more advice:

  1. Since i have many subdomains for each rules is there a better way than add the rules in looping

    foreach($subdomains as $a){ $tr = new Zend_Controller_Router_Route_Hostname( "$a.site.com", array( 'module' => 'mod', 'controller' => 'ctrl', 'param_1' => $a )); $router->addRoute($a,$tr); }

  2. How to combine it with other routing type to parse the parameters (chained?), something like http://a.site.com/:b/:c, i want t parse it to param_1 (a), param_2 (b), param_2 (c)

like image 295
Komang Avatar asked Nov 10 '10 07:11

Komang


1 Answers

Note: Reverse Matching
Routes are matched in reverse order so make sure your most generic routes are defined first.

(Zend_Controller_Router)

Thus you have to define the route for all other subdomains first, then the specific ones:

$user = new Zend_Controller_Router_Route_Hostname(
    ':subdomain.site.com',
    array(
        'controller' => 'user'
    )
);
$location1 = new Zend_Controller_Router_Route_Hostname(
    'c.site.com',
    array(
        'controller' => 'location'
    )
);
$location1 = new Zend_Controller_Router_Route_Hostname(
    'd.site.com',
    array(
        'controller' => 'location'
    )
);
// other definitions with known subdomain
$router->addRoute($user);   // most general one added first
$router->addRoute($location1);
$router->addRoute($location2);
// add all other subdomains

Update for the updated question:
1) This really depends on how different the parameters are you want to route a subdomain to. In your example you routed them all to the same model and controller and added the actual subdomain as a parameter. This can be done easily with the user-route i posted above. There the subdomain is set as parameter subdomain ($request->getParam("subdomain")). If you want the subdomains to be the action of a known controller/model you could replace :subdomain with :action. But as soon as you have other controllers/models for each subdomain, I'm affraid you have to loop over them (or use a config file). For the example you provided in the question, the route simply could look like this:

$user = new Zend_Controller_Router_Route_Hostname(
    ':param1.site.com',
    array(
        'controller' => 'user'
    )
);
// routes "subdomain".site.com to defaultModul/userController/indexAction with additional parameter param1 => subdomain.

As long as you don't have any schema in your subdomains it's very difficult to route them in a general way.

2) That's an example where router chains come into play. The outer route would be the hostname route which handles the subdomain and the inner route would handle the :a/:b part. This could look like this for example:

$user->chain(new Zend_Controller_Router_Route(':a/:b'));
like image 124
Fge Avatar answered Nov 03 '22 07:11

Fge