FrontController plugin and controllers

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

FrontController plugin and controllers

asagala
I currently have a FrontController plugin that extends Zend-Controller_Plugin_Abstract. The preDispatch function is implemented in it. Everything works except for one thing. How can I prevent the preDispatch function to be applied to certain controllers? The only way I have right noew is to use $this->getRequest()->getControllerName() and compare it to a list of controller names.

Is there a better/easier way?
Reply | Threaded
Open this post in threaded view
|

Re: FrontController plugin and controllers

Jurian Sluiman-4
On Wednesday 24 Mar 2010 17:27:06 asagala wrote:
> I currently have a FrontController plugin that extends
> Zend-Controller_Plugin_Abstract. The preDispatch function is implemented in
> it. Everything works except for one thing. How can I prevent the
> preDispatch function to be applied to certain controllers? The only way I
> have right noew is to use $this->getRequest()->getControllerName() and
> compare it to a list of controller names.
>
> Is there a better/easier way?

I can think of two options, but both not ideal. The first one is using the
routeShutdown to fetch the route (and thus controller) and based on that info
turn the plugin on or off.

The second is the option you use the controller's preDispatch() method. To
avoid DRY the code inside the method could be replaced with an action helper.

Furthermore, I have no idea...
Regards, Jurian
--
Jurian Sluiman
CTO Soflomo V.O.F.
http://soflomo.com
Reply | Threaded
Open this post in threaded view
|

Re: FrontController plugin and controllers

asagala
What do you mean by this "To avoid DRY the code inside the method could be replaced with an action helper. "

Do you mean implement my frontcontroller logic as an action helper and call the action helper in preDispatch function on the controllers I need it?
Reply | Threaded
Open this post in threaded view
|

Re: FrontController plugin and controllers

A.J. Brown-3
In reply to this post by Jurian Sluiman-4
The way you're doing it is probably the best.  The only other
alternative I can think of is to have another plugin at
routeShutdown() which only registers the plugin for select
controllers.  That seems to be a more convoluted solution than to just
have your plugin's code determine if it should do anything.  The
controller is not known untill after routing has taken place.


On Wed, Mar 24, 2010 at 1:07 PM, Jurian Sluiman
<[hidden email]> wrote:

> On Wednesday 24 Mar 2010 17:27:06 asagala wrote:
>> I currently have a FrontController plugin that extends
>> Zend-Controller_Plugin_Abstract. The preDispatch function is implemented in
>> it. Everything works except for one thing. How can I prevent the
>> preDispatch function to be applied to certain controllers? The only way I
>> have right noew is to use $this->getRequest()->getControllerName() and
>> compare it to a list of controller names.
>>
>> Is there a better/easier way?
>
> I can think of two options, but both not ideal. The first one is using the
> routeShutdown to fetch the route (and thus controller) and based on that info
> turn the plugin on or off.
>
> The second is the option you use the controller's preDispatch() method. To
> avoid DRY the code inside the method could be replaced with an action helper.
>
> Furthermore, I have no idea...
> Regards, Jurian
> --
> Jurian Sluiman
> CTO Soflomo V.O.F.
> http://soflomo.com
>



--
A.J. Brown
Software Engineer, ZCE
blog : http://ajbrown.org
talk  : (937) 540-0099
chat : IntypicaAJ
Reply | Threaded
Open this post in threaded view
|

Re: FrontController plugin and controllers

Jurian Sluiman-4
In reply to this post by asagala
On Wednesday 24 Mar 2010 18:24:00 asagala wrote:
> What do you mean by this "To avoid DRY the code inside the method could be
> replaced with an action helper. "
>
> Do you mean implement my frontcontroller logic as an action helper and call
> the action helper in preDispatch function on the controllers I need it?

Yes, that's what I mean. If you only need the code in 2 out of 10 controllers,
it's imho better to do like this:

//nothing
class IndexController {}
// helper
class FooController
{
  public function preDispatch() {some_action_helper():}
}
//helper
class BarController
{
  public function preDispatch () {some_action_helper();}
}
//nothing
class BazController {}

Having 5 out of 10 controller the required methods, I'd let the code stay as
you had it. If you have 8/10 controllers for the function, I'd look to disable
the plugin at routeShutdown().
--
Jurian Sluiman
CTO Soflomo V.O.F.
http://soflomo.com