Calling Component method on all controllers in Cakephp 3

Asked

Viewed 160 times

0

I’d like to know how to call a method from a Component I created in Cakephp 3 on all controllers, as well as the Auth that checks whether the user is logged in or not on all pages (controllers). But I also want to be able to call another method from this one on some controllers on beforeFilter before it runs the default method that always var be executed.

Example:

On some controllers on beforeFilter I allow some of his actions with the $this->Auth->allow('add') and so when the Authcomponent will check whether the User is logged in or not it ignores why the page was set as allowed, I need to do basically the same with the Component I created.

I want to perform certain standard action on all controllers, but be able to set some settings on beforeFilter of any controller before the default action determinity is executed.

1 answer

0


I’ll leave here the way I solved my problem/dilemma, in case someone needs to do the same thing I do.

In the beforeFilter() from Appcontroller, I called the method I created from my custom Component so that it runs on all pages as follows:

public function beforeFilter(Event $event)
{
    //some code here...
    $this->MeuComponent->metodoPadrao();
}

Then in any specific Controller where I want some other method to be executed before the metodoPadrao(), I call this method on beforeFilter() of the Controller as follows:

public function beforeFilter(Event $event)
{
    $this->MeuComponent->metodoEspecifico();
    parent::beforeFilter($event);
    //some code here...
}

So Cakephp runs the metodoEspecifico() before exhuming the metodoPadrao(), in the controllers where it is "called" before the parent::beforeFilter($event).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.