Skip to content

Instantly share code, notes, and snippets.

@andrewhavens
Created July 10, 2012 15:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
}
}
}
@Bittarman
Copy link

Why use the Zend namespace? are you monkey patching your library?

@andrewhavens
Copy link
Author

No, I changed the name for the example. The real code is actually prefixed with our company name.

@Bittarman
Copy link

So why not share it with your namespace? it makes sense to do so, that way people can use your code without having to ruin their upgrade path by dropping your code into their 'zend' dir in their lib.

@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