How to call Controller function from another Controller Laravel

We normally define Controller in Laravel and call them in blade view files. However, there can be some rare circumstances when in an application, we want to call Controller function from another Controller in a Laravel application.

Laravel Framework provides us with several methods for this. Let’s create a controller with two public methods to access them in another Controller. Normally we set a Controller function as protected, but to make it accessible to the other Controller.

Consider the Controller below with function details to be called in another Controller.

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class FirstController extends Controller
{
    public function Function1()
    {
        return "From First Controller Text";
    }
  
    static function Function2Static()
    {
        return "From First Controller Text";
    }
}

You can access functions from this Controller in three different ways. Let’s discuss them.

Methods to Call Controller function

Method 1

The second Controller will have the code as shown below.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Http\Controllers\FirstController;
  
class SecondController extends Controller
{
    public function index(Request $request)
    {
        $result = (new FirstController )->Function1();
        dd($result);
    }
}

We should add an instance for the first Controller as shown above in the line use App\Http\Controllers\FirstController;

This makes the second Controller access all public functions inside it. To refer to a function in the first controller you will make a call to it and pass the function with an arrow key as shown above. The function result is assigned to the $result variable. Hence, the following output is obtained.

From First Controller Text

Method 2

In this method, you will add the following code to your second Controller.

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class SecondController extends Controller
{
    public function index(Request $request)
    {
        $result = app('App\Http\Controllers\FirstController')->Function1();
        dd($result);
    }
}

Notice the slight difference here. When you don’t add an instance to your First Controller, you can pass it directly inside the index function in the app method as shown above. Here you need to define the path to your Controller as you are not defining it at the top. Similarly, pass the function to be called from this Controller with the arrow key and get same result.

Method 3

The third way is the most simple one. You don’t need to add any instance, neither call the entire path in the index function. Just use the simple code as shown below, but use the static function. As this method supports static functions. You will get the same result as output.

<?php
  
namespace App\Http\Controllers;
use Illuminate\Http\Request;
  
class SecondController extends Controller
{
    public function index(Request $request)
    {
        $result = FirstController::Function2Static();
        dd($result);
    }
}

Leave a Comment