(on Thursday, 04 December 2008, 01:58 PM +0000):
> I am trying to have my routes set via a plugin, but even though it seems
> like the front controller is having the router updated and assigning the
> router is in the routeStartup method of my plugin, my routes don't seem
> to work.
>
> My plugin looks like:
>
>
> class Broadcast_Controller_Plugin_Routes extends
> Zend_Controller_Plugin_Abstract
> {
> /**
> * Called before Zend_Controller_Front begins evaluating the
> * request against its routes.
> *
> * @param Zend_Controller_Request_Abstract $request
> * @return void
> */
> public function routeStartup(Zend_Controller_Request_Abstract $request)
> {
> $router = new Zend_Controller_Router_Rewrite();
> $router->addRoute('login',
> new Zend_Controller_Router_Route_Static('login',
> array(
> 'module' => 'default',
> 'controller' => 'index',
> 'action' => 'index'
> )
> )
> );
> $router->addRoute('logout',
> new Zend_Controller_Router_Route_Static('logout',
> array(
> 'module' => 'default',
> 'controller' => 'index',
> 'action' => 'logout'
> )
> )
> );
> Zend_Controller_Front::getInstance()->setRouter($router);
> }
> }
>
>
> I have other routes in there but limited it to only two for brevity.
>
> Should this work? Am I just doing something stupidly wrong? Any advice
> is appreciated!
This won't work as written. and actually shows a slight flaw in how
dispatch() is written. Basically, while you can *add* routes during
routeStartup(), you cannot replace the router as it's already been
pulled into the local scope in the dispatch() method.
What you *can* do, however, is pull the router into your plugin, and add
routes to it. So, in your code, instead of instantiating a new router
object, pull it from the front controller:
$router = Zend_Controller_Front::getInstance()->getRouter();
// now add the routes..
--
Matthew Weier O'Phinney
Software Architect | matthew@zend.com
Zend Framework | http://framework.zend.com/
没有评论:
发表评论