Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Call Controller Method from Another Controller in Laravel

How to Call Controller Method from Another Controller in Laravel ?

Aman Jain, December 2, 2023March 16, 2024

In this article we will learn Call Controller Method from Another Controller in Laravel. Sometimes we need to access the controller method from another controller to save the same code on multiple locations. Best way to achieve it is using create a separate service class or trait in PHP so that we can import the class in both controllers but in case due to lack of time or any reason we can’t able to create these class than larvae also offers to call controller function or method from another controller.

Method 1 : Call Controller Method from Another Controller in Laravel

Suppose you have a controller class name TestAController and another class TestBController, Class TestAController contains a method print() and you want to access the print method in class b so

<?php 

class TestAController extends Controller{

  function print(){
   return "print";  
  }
}

class TestBController extends Controller{
 
   function testB(){
    app('App\Http\Controllers\TestAControler')->print();
   }

}

Here we used laravel app function to create the object of TestAController class and then called the print method.

it’s generally not recommended to directly call a method from one controller in another controller. Controllers in Laravel are designed to handle HTTP requests and responses, and they should remain independent of each other.

Method 2 : Inherit the class by extending the parent class

In this method we will inherit class TestAController to class TestBController as below

<?php 

class TestAController extends Controller{

  function print(){
   return "print";  
  }
}

class TestBController extends TestAController{
 
   function testB(){
     return $this->print();
   }

}

Method 3 : Using the trait to share the same methods in controllers

Directly calling one controller method from another controller in Laravel is generally not recommended, as controllers should be kept thin and focused on handling HTTP requests and responses. However, if you have a specific use case that requires this, you can achieve it by using traits or by extending a base controller class.

So create a trait in folder app-> traits if you do not have folder then create the folder first.

<?php 
namespace App\Traits;
trait CommonMethods{

 function print(){
   return "print";  
  }

}

now us it in both controlllers as below

<?php 

class TestAController extends Controller{
use App\Traits\CommonMethods;
  function method1(){
   return $this->print();  
  }
}

class TestBController extends TestAController{
 use App\Traits\CommonMethods;
  function method2(){
   return $this->print();  
  }

}

Method 4 : Create object of Controller

in this method we will create the object of first controller and then will use it in B controller

<?php 

class TestAController extends Controller{

  function print(){
   return "print";  
  }
}

class TestBController extends Controller{
 
   function testB(){
    $aContoller = new \App\Http\Controllers\TestAControler;
    $aContoller->print();
   }

}

Related

Laravel Uncategorized

Post navigation

Previous post
Next post

Related Posts

Php Laravel 8 generate qr code

How to generate qr code in Laravel 8?

October 16, 2021November 16, 2021

In this article we will learn to generate the qr code in Laravel. we will use a simple example to generate the qr code. As we know qr code is stand for Quick Response and we can scan and get the information from it. we can use qr code in…

Read More
Php Laravel Rest Api OTP Login and Registration Without Password

Laravel Rest Api OTP Login and Registration Without Password

September 3, 2022March 16, 2024

Today most of the applications are using laravel rest api otp login and registration without password for ease of login and registration and also provides easy to login without remembering the password each time while login or register. In Laravel there is multiple ways to implement the login and registration…

Read More
Php How to print raw sql query in eloquent laravel

How to print raw sql query in eloquent laravel ?

November 23, 2022March 16, 2024

In this article we will learn to print raw sql query in eloquent laravel. Sometime to debug the MySQL query in laravel eloquent we need to echo raw sql query and want to know exact query. In laravel we have multiple methods to print database query in laravel 8 and…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version