Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Call custom function from Controler

Hello,

i have a UserController and a MembershipController with a fuction called checkMembership.

After login i will check in UserController indexAction() if Membership exists, something like that:

$this->membership->checkMembership();

But it doesn't work, so i can't call the checkMembership() function in UserController indexAction(). I have a ControllerBase, so each Controller know each other.

Thx

Its a bad practice to call a function from one to another controller. You have two options:

1) Use a BaseController which has the common functions which other controllers can reuse, by extending the BaseController.

class MembershipController extends ... 
{
    public function checkMembership()
    {
        return ...
    }

}

class UserController extends MembershipController
{
    public function indexAction()
    {
        $this->checkMembership();
    }
}

2) Remove the method from MembershipController and place it in a Model, Library or a Helper.

class MembershipHelper 
{
    public static function checkMembership()
    {
        return ...
    } 
}

class UserController extends MembershipController
{
    public function indexAction()
    {
        \Helpers\MembershipHelper::checkMembership();
    }
}

Of course namespaces and class names are just for example. You will have to modify according to your project structure.



32.1k

Thx for your help :-)

is it 1 or 2 or is it 1 and 2 :-)

I have a BaseController! My UserController extends BaseController, so what can i do now?



32.1k
Accepted
answer

i got it!

I put the checkMembership(); function into the Auth class which extends Components. Now i can call it like this $this->auth->checkMembership();

Is that ok? My Base System is Vokuro.

Both 1 and 2 are OK to use. In your case (2) is better, since its a functionality about user authentication and its best placed in the Auth class :)



32.1k

Think that my page will go online very soon, it was a big fight for me :-)

Thx a lot!!!!!!!