front controller.
You may assume that from a plugin you can do:
$front = Zend_Controller_Front::getInstance();
$front->setParam('my','param');
However this wont work for the same reason that setting the router
wont, as the params have already been set in the dispatcher before the
dispatch begins:
Front Controller source:
/**
* Initialize router
*/
$router = $this->getRouter();
$router->setParams($this->getParams());
/**
* Initialize dispatcher
*/
$dispatcher = $this->getDispatcher();
$dispatcher->setParams($this->getParams())
->setResponse($this->_response);
// Begin dispatch
So you have to get the dispatcher instead:
$front->getDispatcher()->setParam('my','param');
That one got me for a while until I read the source :)
2008/12/4 Matthew Weier O'Phinney <matthew@zend.com>:
> -- Andrew Collington <php@amnuts.com> wrote
> (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/
>
>
--
----------------------------------------------------------------------
[MuTe]
----------------------------------------------------------------------
没有评论:
发表评论