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 How to create multiple size thumbs from image in laravel

How to create multiple size thumbs from image in laravel 9 ?

May 14, 2022February 22, 2024

Resizing images can be a best performance booster for any application since this will load the proper size images. In this article i will show you to create multiple size thumbs from image in laravel using intervention package. Big size of image can reduce the performance of the application therefor…

Read More
Php How to Check folder exist and Create a Nested Folder in Laravel

How to Check folder exist and Create Nested Folder in Laravel ?

May 24, 2022May 26, 2022

This article will guide you to Check folder exist and Create Nested Folder in Laravel. To perform the file system based operations like Check folder exist and Create a Nested Folder. we use File class in laravel. Laravel provides inbuilt library to access the file system and we can do…

Read More
Laravel How to rollback migration laravel

How to Rollback Migration Laravel ?

March 6, 2022February 8, 2024

In our recent article I showed you about creating migration in laravel and sometimes after creating migration we want to rollback migration laravel so in this article i will show you to rollback migration in laravel. Laravel artisan have multiple options to rollback the migration like we can rollback all…

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