Laravel: Load method in another controller without changing the url

前端 未结 4 1460
清歌不尽
清歌不尽 2020-12-07 21:20

I have this route: Route::controller(\'/\', \'PearsController\'); Is it possible in Laravel to get the PearsController to load a method from another controller

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-07 22:08

    You can use (L3 only)

    Controller::call('ApplesController@getSomething');
    

    In L4 you can use

    $request = Request::create('/apples', 'GET', array());
    return Route::dispatch($request)->getContent();
    

    In this case, you have to define a route for ApplesController, something like this

    Route::get('/apples', 'ApplesController@getSomething'); // in routes.php
    

    In the array() you can pass arguments if required.

提交回复
热议问题