Access Controller method in Laravel
How to access a method in a controller from another controller. Lets discuss that in this session. Lets say the controllers are PrintReportController and SubmitPerformanceController. There is an easy way.
app('App\Http\Controllers\PrintReportController')->getPrintReport();
This is bad in the case of code organization.
Creating a trait in app/Traits will implement the logic there and ask your controllers to use it.
trait PrintReport {
public function getPrintReport() {
// .....
}
}
This will create trait now tell the controllers to use it as given below.
class PrintReportController extends Controller {
use PrintReport;
}
class SubmitPerformanceController extends Controller {
use PrintReport;
}
Both of the above methods will make SubmitPerformanceController to have getPrintReport method. Now call $this->getPrintReport(); from the controller or from the routes.
Comments
0 comments
Please Sign in or Create an account to Post Comments