2010-11-19 78 views
6

我有一大堆我需要重定向到新路線的舊路線。Zend Framework:如何將舊路由重定向到新的自定義路由?

我已經定義我的自定義路線的引導:

protected function _initRoutes() 
{ 
    $router = Zend_Controller_Front::getInstance()->getRouter(); 

    $oldRoute = 'old/route.html'; 
    $newRoute = 'new/route/*'; 

    //how do I add a 301 redirect to the new route? 

    $router->addRoute('new_route', 
     new Zend_Controller_Router_Route($newRoute, 
      array('controller' =>'fancy', 'action' => 'route') 
    )); 
} 

如何添加重定向使用301重定向到新航線的老航路?

+0

爲什麼發送301沒有做重定向是什麼時候?從我所看到的只是將舊路線映射到新路線而不重定向? – Fge 2010-11-19 20:49:59

+0

對不起,不好的例子。我正在嘗試重定向,而不是路由。 – Andrew 2010-11-19 20:51:54

+0

我已更新示例以更清晰 – Andrew 2010-11-19 20:54:13

回答

7

Zend框架沒有這種類型的內置功能。所以,我創建爲了處理一個自定義路由對象是:

class Zend_Controller_Router_Route_Redirect extends Zend_Controller_Router_Route 
{ 
    public function match($path, $partial = false) 
    { 
     if ($route = parent::match($path, $partial)) { 
      $helper = new Zend_Controller_Action_Helper_Redirector(); 
      $helper->setCode(301); 
      $helper->gotoRoute($route); 
     } 
    } 
} 

然後,你可以定義你的路由時使用

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initCustomRoutes() 
    { 
     $router = Zend_Controller_Front::getInstance()->getRouter(); 
     $route = new Zend_Controller_Router_Route_Redirect('old/route/*', array('controller'=>'content', 'action'=>'index'));  
     $router->addRoute('old_route', $route); 
    } 
} 
0

有幾種方法可以解決這個問題。

最簡單可能是隻使用你的.htaccess文件做一個RewriteRule pattern substitution [R=301]

你也可以檢測控制器使用什麼途徑和重定向依據是:

public function preDispatch() { 
    $router = $this->getFrontController()->getRouter(); 
    if ($router->getCurrentRouteName() != 'default') { 
    return $this->_redirect($url, array('code'=>301)); 
    } 
} 
9

我像這樣做

  1. 添加Zend_Route_Regexp路線作爲舊路線
  2. 添加控制器和行動的老路
  3. 添加邏輯來解析老路
  4. 添加$this->_redirect($url, array('code' => 301))這個邏輯
+0

我想這就是我要做的。謝謝! – Andrew 2010-11-19 22:25:53

0

我用來做到這一點的方式是重定向到只處理重定向的控制器。現在我使用我的其他答案中提到的自定義類。

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{ 
    protected function _initRoutes() 
    { 
     $router = Zend_Controller_Front::getInstance()->getRouter(); 

     //new routes 
     $router->addRoute('myroute', 
      new Zend_Controller_Router_Route_Static('/new/route/1234', 
       array('controller' =>'brands', 'action' => 'view', 'id' => '4') 
     )); 

     //old routes 
     $oldRoutes = array(
      '/old/route/number/1' => '/new/route/1234', 
     } 
     foreach ($oldRoutes as $oldRoute => $newRoute) { 
      $router->addRoute($oldRoute, new Zend_Controller_Router_Route_Static($oldRoute, array('controller' =>'old-routes', 'action' => 'redirect', 'new-route' => $newRoute))); 
     } 
    } 
} 

而且控制器:

class OldRoutesController extends Zend_Controller_Action 
{  
    public function redirectAction() 
    { 
     $newRoute = $this->_getParam('new-route'); 
     return $this->_redirect($newRoute, array('code' => 301)); 
    } 
} 
2

在控制器,試試這個方法:

 $this->getHelper('redirector')->setCode(301); 
    $this->_redirect(...);