代码之家  ›  专栏  ›  技术社区  ›  Andrew

Zend框架:如何301将旧路由重定向到新的自定义路由?

  •  6
  • Andrew  · 技术社区  · 14 年前

    我有一个旧路线的大列表,我需要重定向到新路线。

    我已经在引导中定义了我的自定义路由:

    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重定向将旧路由重定向到新路由的路由?

    5 回复  |  直到 12 年前
        1
  •  10
  •   Luís Cruz    7 年前

    我是这样做的

    1. 将Zend_Route_Regexp路由添加为旧路由
    2. 为旧路由添加控制器和操作
    3. 添加逻辑以分析旧路由
    4. 添加 $this->_redirect($url, array('code' => 301)) 为了这个逻辑
        2
  •  7
  •   Andrew    12 年前

    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);
        }
    }
    
        3
  •  2
  •   Krzysztof Cuber    11 年前

    在控制器中,尝试以下方法:

         $this->getHelper('redirector')->setCode(301);
         $this->_redirect(...);
    
        4
  •  0
  •   gnarf    14 年前

    有几种方法可以解决这个问题。

    这个 最简单的 可能只是用你的 .htaccess 要执行的文件 RewriteRule pattern substitution [R=301]

    您还可以检测控制器中使用的路由,并基于此重定向:

    public function preDispatch() {
      $router = $this->getFrontController()->getRouter();
      if ($router->getCurrentRouteName() != 'default') {
        return $this->_redirect($url, array('code'=>301));
      }
    }
    
        5
  •  0
  •   Andrew    12 年前

    我以前的方法是重定向到只处理重定向的控制器。现在我使用另一个答案中提到的自定义类。

    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));
        }
    }