Access Controller method from another controller i

2019-01-04 18:09发布

I have two controllers SubmitPerformanceController and PrintReportController.

In PrintReportController I have a method called getPrintReport.

How to access this method in SubmitPerformanceController?

9条回答
Root(大扎)
2楼-- · 2019-01-04 18:59
namespace App\Http\Controllers;

//call the controller you want to use its methods
use App\Http\Controllers\AdminController;

use Illuminate\Http\Request;

use App\Http\Requests;

class MealController extends Controller
   {
      public function try_call( AdminController $admin){
         return $admin->index();   
    }
   }
查看更多
beautiful°
3楼-- · 2019-01-04 19:00
\App::call('App\Http\Controllers\MyController@getFoo')
查看更多
Evening l夕情丶
4楼-- · 2019-01-04 19:01

You shouldn’t. It’s an anti-pattern. If you have a method in one controller that you need to access in another controller, then that’s a sign you need to re-factor.

Consider re-factoring the method out in to a service class, that you can then instantiate in multiple controllers. So if you need to offer print reports for multiple models, you could do something like this:

class ExampleController extends Controller
{
    public function printReport()
    {
        $report = new PrintReport($itemToReportOn);
        return $report->render();
    }
}
查看更多
登录 后发表回答