Skip to content

Instantly share code, notes, and snippets.

@andrewhavens
Created July 10, 2012 15:20
Show Gist options
  • Save andrewhavens/3084009 to your computer and use it in GitHub Desktop.
Save andrewhavens/3084009 to your computer and use it in GitHub Desktop.
An example of a custom Zend Framework route that results in a 301 redirect
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initCustomRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Mylib_Controller_Router_Route_Redirect('old/route/*', array('controller'=>'content', 'action'=>'index'));
$router->addRoute('old_route', $route);
}
}
<?php
class Mylib_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);
}
}
}
@andrewhavens
Copy link
Author

I assumed the reader would already know how to do that, but I have renamed the code sample to be more explicit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment