I have two controllers SubmitPerformanceController
and PrintReportController
.
In PrintReportController
I have a method called getPrintReport
.
How to access this method in SubmitPerformanceController
?
I have two controllers SubmitPerformanceController
and PrintReportController
.
In PrintReportController
I have a method called getPrintReport
.
How to access this method in SubmitPerformanceController
?
You can access your controller method like this:
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
soSubmitPerformanceController
will inherit that methodBut this will also inherit all other methods from
PrintReportController
.The best approach will be to create a
trait
, implement the logic there and tell your controllers to use it:Tell your controllers to use this trait:
Both solutions make
SubmitPerformanceController
to havegetPrintReport
method so you can call it with$this->getPrintReport();
from within the controller or directly as a route (if you mapped it in theroutes.php
)You can read more about traits here.
If you need that method in another controller, that means you need to abstract it and make it reusable. Move that implementation into a service class (ReportingService or something similar) and inject it into your controllers.
Example:
Do the same for the other controllers where you need that implementation. Reaching for controller methods from other controllers is a code smell.
First of all, request method of controller from another controller is EVIL. This will cause many hidden problems of lifecycle in Laravel.
Anyway, there are many solutions for doing that. You can select one of these various ways.
Case 1) If you want to call based on Classes
Way 1) The simple way
But you can't any parameters or authentication with this way.
Way 2) Make devide the service logics in controllers.
You can any parameters and something with this. The best solution for your programming life. You can make
Repository
insteadService
.Case 2) If you want to call based on Route
Way 1) Use
MakesHttpRequests
trait that used in Application Unit Testing.I recommend if you have special reason for making this proxy. You can use any parameters and custom headers. Also this will be internal request in laravel. (Fake HTTP Request) You can see more detail manuals for
call
method in here.However this is not 'good' solution, too.
Way 2) Use guzzlehttp client
The most terrible solution I think. You can use any parameters and custom headers, too. But this making external extra http request. So HTTP Webserver must be running.
Finally I am using Way 1 of Case 2. I need parameters and
Here the trait fully emulates running controller by laravel router (including support of middlewares and dependency injection). Tested only with 5.4 version
Then just add it to your class and run the controller. Note, that dependency injection will be assigned with your current route.
Laravel 5 compatible method
Note: this will not update the URL of the page.
It's better to call the Route instead and let it call the controller.
Late reply, but I have been looking for this for sometime. This is now possible in a very simple way.
Without parameters
With Parameters
Docs: https://laravel.com/docs/5.6/responses#redirecting-controller-actions
Back in 5.0 it required the entire path, now it's much simpler.