Access Controller Method from Another Controller in Laravel 5

Access Controller method from another controller in Laravel 5

You can access your controller method like this:

app('App\Http\Controllers\PrintReportController')->getPrintReport();

This will work, but it's bad in terms of code organisation (remember to use the right namespace for your PrintReportController)

You can extend the PrintReportController so SubmitPerformanceController will inherit that method

class SubmitPerformanceController extends PrintReportController {
// ....
}

But this will also inherit all other methods from PrintReportController.

The best approach will be to create a trait (e.g. in app/Traits), implement the logic there and tell your controllers to use it:

trait PrintReport {

public function getPrintReport() {
// .....
}
}

Tell your controllers to use this trait:

class PrintReportController extends Controller {
use PrintReport;
}

class SubmitPerformanceController extends Controller {
use PrintReport;
}

Both solutions make SubmitPerformanceController to have getPrintReport method so you can call it with $this->getPrintReport(); from within the controller or directly as a route (if you mapped it in the routes.php)

You can read more about traits here.

How to Call a controller function in another Controller in Laravel 5

Let's say I have Controller1 and Controller2. I want to call a function of Controller1 from inside a function placed in Controller2.

// Controller1.php
class Controller1 {
public static function f1()
{

}
}

And on the other controller:

// Controller2.php
use App\Http\Controllers\Controller1;

class Controller2 {
public function f2()
{
return Controller1::f1();
}
}

Points to be noted:

  1. f1() is declared static

controller use another controller

I agree with @ptrTon on this, I'd suggest adopting a Repository pattern. It may be a bit of a work depending on your app size, but it's defenitely cleaner than instantiating a controller inside another controller.

Basically with this approach you don't manipulate your model directly but you use instead an object which is, in fact, an extra layer. The main advantage of this is that you can extract common operations and perform them from anywhere not inside the controller, but inside an object which the only responsability is to manage the operations on your models, further separating the responsabilities of your app components. With Laravel you can also add a custom route resolution logic which will inject these repositories in your controllers using the IoC container.

If you want to take it further, you could create the repository in a way that it behaves as the model which wraps, and extends its functionalities (PHP's magic methods are your friends). Providing a full example in a single answer can be complicated, but I'll link some intresting resources below.

Repository pattern in Laravel - example

Decorator pattern - concepts

Laravel's explicit model binding (see the "customizing resolution logic" section)

Call controller function in another controller

I think your function should be in one of your model since you're only getting value.

This will me more relevant according to MVC.

Having it in your User model will let you use it on every instance of your application with a single call.

Passing variable to method from another controller

You should not call controllers inside another controllers.

You are facing this problem because you are putting business logic inside your controllers (and it is a well known bad practice).

The solution is to use another class, called a service class, where you put the logic. You then call this class where you need to (in your case, in PrenotazioniSpedizioniController and ExportFileController).

This is a quick example:

Create the file app\Services\ExportService.php:

class ExportService {
public function exportXML($arguments)
{
//something
}
}

Then, in your controllers:

use App\Http\Controllers\ExportFileController;

class PrenotazioniSpedizioniController extends Controller
{
public function yourMethod(Request $request){
app(ExportService::class)->exportXML($arguments);
}
}

This way you keep clean controllers and have a more testable code.

Keep in mind this is a minimal example to solve your problem and I can only advise you to dig further on project architecture and design patterns.



Related Topics



Leave a reply



Submit