Laravel:在另一个控制器中加载方法而不更改 url

2022-01-05 00:00:00 php laravel controller

我有这个路由:Route::controller('/', 'PearsController'); 在 Laravel 中是否有可能让 PearsController 从另一个控制器加载一个方法,这样 URL 就不会'改变?

I have this route: Route::controller('/', 'PearsController'); Is it possible in Laravel to get the PearsController to load a method from another controller so the URL doesn't change?

例如:

// route:
Route::controller('/', 'PearsController');


// controllers
class PearsController extends BaseController {

    public function getAbc() {
        // How do I load ApplesController@getSomething so I can split up
        // my methods without changing the url? (retains domain.com/abc)
    }

}

class ApplesController extends BaseController {

    public function getSomething() {
        echo 'It works!'
    }

}

推荐答案

您可以使用(仅限 L3)

You can use (L3 only)

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

L4中你可以使用

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

在这种情况下,你必须为ApplesController定义一个路由,就像这样

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

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

array() 中,如果需要,您可以传递参数.

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

相关文章