Zend Framework Hostname Routes: Routing while igno

2019-08-06 04:21发布

So I want to create a Zend_Controller_Router_Route_Hostname that matches the following:

  • example.com
  • www.example.com
  • stage.example.com
  • dev.example.com
  • andrew.dev.example.com
  • joe.dev.example.com

I can't seem to figure out how to create a route that can match all these things. There can be multiple subdomains before "example.com", so everything before it doesn't really matter. Here's what I have so far:

$hostnameRoute = new Zend_Controller_Router_Route_Hostname(':sandbox.:environment.example.com', array('controller' => 'events', 'event-id' => $eventId));

2条回答
等我变得足够好
2楼-- · 2019-08-06 04:39

I don't think it's possible to have multiple subomains match in a single hostname route. So here's what you have to do:

$hostnameRoutes = array(
    new Zend_Controller_Router_Route_Hostname('example.com', array('controller' => 'events', 'event-id' => $eventId)),
    new Zend_Controller_Router_Route_Hostname(':subdomain.example.com', array('controller' => 'events', 'event-id' => $eventId)),
    new Zend_Controller_Router_Route_Hostname(':sandbox.dev.example.com', array('controller' => 'events', 'event-id' => $eventId))
);

$homepageRoute =         new Zend_Controller_Router_Route_Static('',       array('action' => 'overview'));
$eventPagesRoute =       new Zend_Controller_Router_Route(':action/*',     array('action' => 'overview'));
$staticEventPagesRoute = new Zend_Controller_Router_Route('page/:page-id', array('action' => 'static-page'));

foreach ($hostnameRoutes as $i => $hostnameRoute) {
    $router->addRoute('sports_homepage' . $i,          $hostnameRoute->chain($homepageRoute));
    $router->addRoute('sports_event_pages' . $i,       $hostnameRoute->chain($eventPagesRoute));
    $router->addRoute('sports_static_event_Page' . $i, $hostnameRoute->chain($staticEventPagesRoute));
}

Note: make sure the names of your routes are unique (note the $i being concatenated to make them unique). I forgot about that first time through and couldn't figure out why it wasn't working.

查看更多
小情绪 Triste *
3楼-- · 2019-08-06 04:40

You can't one route for non-linear mapping with more then two choices, so you'll have to use more then one ;) Without knowing the targets of each domain it's hard to give any examples. Assuming example.com and www.example.com are eaqual and are both the default route and stage and dev are both mapped to their respecitve module (and andreaw and joe as parameters) (and added to the router of course):

$stageRoute = new Zend_Controller_Router_Route_Hostname('stage.example.com', array('module' => 'stage');
$devRoute = new Zend_Controller_Router_Route_Hostname(':user.dev.example.com', array('module' => 'dev');

This will map the following (:module/:controller/:action):

  • stage.example.com to stage/index/index
  • dev.example.com to dev/index/index
  • joe.example.com to dev/index/index/user/joe
  • www.example.com to default/index/index

In words: if the sudomain is stage and nothing else, go to the stage module with default parameters. If the subdomain is dev go to the dev module with parameter user. Else use the default zend route.
Be careful that hostname routers do match any path. So you probably should match the path behind it with another route and using the chain router.

For the stage route this could look like this:

$stageRoute = new Zend_Controller_Router_Route_Hostname('stage.example.com', array('module' => 'stage');
$pathRoute = new Zend_Controller_Router_Route(':revision', array('controller' => 'browse'));
$stageRoute->chain($pathRoute);

That'd map stage.example.com/5434 to stage/browse/index/revision/5434.

查看更多
登录 后发表回答