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

Laravel How to use Post Ajax Request in Laravel 8 9

How to use Post Ajax Request in Laravel 8 / 9?

June 2, 2022June 2, 2022

Post Ajax request in laravel is use to communicate with server without reloading the page. Ajax requests are useful when we do not want to refresh the page and update the content of page so in that case Ajax request are very useful. Sometimes its also useful while submitting a…

Read More
Php Laravel custom login and register

Laravel 8 Custom login and registration with example

December 3, 2021December 3, 2021

In Laravel 8 there is multiple ways to implement the login and registration like Laravel provides its own auth with packages Jetstream, passport,sanctum, breeze and fortify. These all packages are easy to install and configure but sometimes our application requirement and design patterns are different or we can say we…

Read More

How to print database query in laravel 8 ?

January 1, 2022February 22, 2024

Sometime to debug the MySQL query in laravel eloquent we need to print the raw MySQL query. In laravel we have multiple methods to print database query in laravel 8 and we can also print the query before executing it. Laravel eloquent method to print the query The first approach…

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

  • August 2025
  • 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

  • The Transformative Power of Education in the Digital Age
  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version