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

satheeshkumarj's avatar

Access Function of another contoller

I need to access function of another controller Controller 1

public function controller_1_function(){
$val = $this->Controller_2->controller_2_function();
}

Controller 2

public function controller_2_function(){
/*code*/
return;
}

But I am getting error Undefined property: "App\Http\Controllers\controller_1::$controller_2_function

0 likes
2 replies
Sinnbeck's avatar

This is a really bad pattern. Instead move the logic to a common class like an action or a job and call that from both places

2 likes
Tray2's avatar

I agree with @sinnbeck that this is a really bad pattern, and not to mention a bad practice.

If you need to to the same thing in two or more controllers, you could push it up the the base controller class. Or you can create a trait that contains the logic, that you can use in the controllers that needs to share that method.

1 like

Please or to participate in this conversation.