Well, it’s been a while since I’ve done a PHP update hasn’t it? Well alls well here in CStat, I have a quickie for you folks.
Recently at work I had the need to run a script before every single controller (namely to add a plugin folder to Dwoo) for a specific module that I did not desire for any other modules.
I could have subclassed all my controllers to extend a custom action controller that handled this in the
init() method, however I’m lazy so I wrote a quick Zend Controller Plugin to handle this for me.
What it does is grabs the module name from the request object on routeShutdown (the routeShutdown happens after the route has been parsed and the request object has been generated, but before the action controller is instantiated and executed). From there it uses
Zend_Font_Controller::getInstance()->getModuleDirectory(...) to grab the literal path to the module directory, tacks on our init file name (in our case “init.php”), and if it exists execute it in its own clean environment.
The init.php file has access to
$this is an instance of the controller plugin (in particular we can do
$this->getRequest() inside the init script).
The code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
As always all code I post on this blog is covered under the MIT license.