Zend Framework: How to 301 redirect old routes to new custom routes?

♀尐吖头ヾ 提交于 2019-12-05 06:57:24
Vadyus

I've done it like this

  1. Add a Zend_Route_Regexp route as the old route
  2. Add Controller and action for the old route
  3. Add logic to parse old route
  4. Add $this->_redirect($url, array('code' => 301)) for this logic

Zend Framework does not have this type of functionality built in. So I have created a custom Route object in order to handle this:

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

Then you can use it when defining your routes:

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

In controller, try this way:

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

There are a few approaches to solve this issue.

The easiest is probably to just use your .htaccess file to do a RewriteRule pattern substitution [R=301]

You could also detect what route was used in the controller and redirect based on that:

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

The way I used to do it was to redirect to a controller that only handled redirects. Now I use the custom class mentioned in my other answer.

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

And the controller:

class OldRoutesController extends Zend_Controller_Action
{    
    public function redirectAction()
    {
        $newRoute = $this->_getParam('new-route');
        return $this->_redirect($newRoute, array('code' => 301));
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!