Created
July 10, 2012 15:20
An example of a custom Zend Framework route that results in a 301 redirect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
} |
No, I changed the name for the example. The real code is actually prefixed with our company name.
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.
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
Why use the Zend namespace? are you monkey patching your library?