Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uccdev's avatar

"Call to undefined method" of a clearly defined method?

I have a class IndexController class with its own method verifyUser.

 public function verifyUser($user) {
     echo "Test!";
  }

Its actual code isn't as important as the nature that it isn't called when I want it to be.

It works perfectly fine in its base class, but when I try to call it in other classes I get different results:

   $this->indexController = new IndexController();
   $this->indexController->verifyUser(auth()->user());

I can confirm that auth()->user() is fine. The error message I get is something else: Call to undefined method: IndexController::verifyUser()

I can reword the call in a few different ways, such as:

 IndexController::verifyUser(auth()->user());

Or

 $thing = $this->indexController->verifyUser(auth()->user());

But it doesn't fit it.

I note that if I simply paste the code that verifyUser has into my new class, it works just fine. It's just this one form of method calling. I don't get why it works. Could anyone shed some light on why I might be getting this issue?

0 likes
4 replies
jlrdw's avatar

When following laravel conventions you shouldn't need the new keyword.

Perhaps reread the the Chapter on Authentication, maybe you're missing something.

Talinon's avatar

@uccdev I would argue that calling a controller method from another controller is bad practice. You should extract the logic to its own service class, or at least a trait.

Now with that being said, it's interesting you are getting a Call to undefined method error, when usually a BadMethodCallException would be thrown on a controller. When you say it works fine from the base class are you talking about an actual parent class? Are you sure you are extending the base class? It looks to me like your IndexController (or your parent class) isn't even extending the base Controller class.

1 like
uccdev's avatar

"I would argue that calling a controller method from another controller is bad practice. You should extract the logic to its own service class, or at least a trait."

Would it do to put the function inside of a Helper class, that both controllers use? That I might store in an App\Helpers directory?

bugsysha's avatar
bugsysha
Best Answer
Level 61

Create a class in some folder within app folder like Verification

namespace App\Verification;

UserVerification {
    public static function verify(User $user) {
        // logic here
    }
}

And then call it from any controller like:

\App\Verification\UserVerification::verify($user);
1 like

Please or to participate in this conversation.