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

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
Php Get Json Post Data in Laravel

How to Get Json Post Data in laravel from Request ?

June 8, 2022February 8, 2024

In this article we will learn to get json post data in laravel from request. Laravel by default supports for form-data and x-www-form-urlencode which we can get easily using the Request class as follow $request->field_name or $request->get(‘field_name’). To retrieve the json post data in laravel we need to call json…

Read More
Php How to set timezone dynamically and globally in Laravel

How to set timezone dynamically and globally in Laravel ?

September 20, 2022March 16, 2024

Laravel stores timezone configurations in config/app.php file and we can easily set timezone dynamically and globally in Laravel. Laravel usage carbon library to get the current date time and format, carbon also respects the timezone defined in config/app.php file. After php 5, php also introduced DateTime class to conduct the…

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

  • 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 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
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version