Laravel 9 How to Call a Controller Function in Another Controller

Reading Time: 4 minutes
5,508 Views

Inside this article we will see the concept i.e Laravel 9 How to Call a Controller Function in Another Controller. Article contains classified information about Calling methods of a controller from another controller.

In case if we have module wise application controllers, then calling methods from one controller to another is the common method to make application more modular and flexible.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Demo – Create Controllers

Open project into terminal and run these commands.

$ php artisan make:controller FirstController

$ php artisan make:controller SecondController

These commands will create two different controllers in application. Controllers will be created inside /app/Http/Controllers folder. Files are: FirstController.php and SecondController.php

Open SecondController.php and write this complete code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SecondController extends Controller
{
    public function exampleFirstFunction()
    {
        return "<h3>'First Function' of Second Controller</h3>";
    }

    static function exampleStaticFunction()
    {
        return "'Static Function' of Second Controller";
    }
}

Inside above controller, you can see we have two methods – exampleFirstFunction() or exampleStaticFunction().

Let’s call these methods into FirstController class.

Method #1: Call Method To Controller

Open FirstController.php and write this code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Http\Controllers\SecondController;
  
class FirstController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $result = (new SecondController)->exampleFirstFunction();
  
        dd($result);
    }
}

Concept

Load Controller

use App\Http\Controllers\SecondController;

Then,

$result = (new SecondController)->exampleFirstFunction();

Method #2: Call Method To Controller

Open FirstController.php and write this code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class FirstController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $result = app('App\Http\Controllers\SecondController')->exampleFirstFunction();
  
        dd($result);
    }
}

Concept

$result = app('App\Http\Controllers\SecondController')->exampleFirstFunction();

Method #3: Call Static Method To Controller

Open FirstController.php and write this code into it.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class FirstController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $result = SecondController::exampleStaticFunction();
  
        dd($result);
    }
}

Concept

$result = SecondController::exampleStaticFunction();

Use :: (scope resolution operator) to call static methods.

We hope this article helped you to learn Laravel 9 How to Call a Controller Function in Another Controller in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.