在laravel官方文檔中有這麼一個路由:
Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { // 這裡如何寫,才能跳轉到控制器呢? });});
注釋的地方怎麼寫才能跳轉到控制器呢,其實也就是說路由裡面的function中,如何跳轉到控制器,例如:跳轉到MainController的index方法
回複內容:
在laravel官方文檔中有這麼一個路由:
Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { // 這裡如何寫,才能跳轉到控制器呢? });});
注釋的地方怎麼寫才能跳轉到控制器呢,其實也就是說路由裡面的function中,如何跳轉到控制器,例如:跳轉到MainController的index方法
Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', 'MainController@index');});
跳轉到MainController的index方法。
index方法似乎應該寫成function ($account,$id){}
可以用make方法:
Route::group(['domain' => '{account}.myapp.com'], function () { Route::get('user/{id}', function ($account, $id) { $app = app(); // 初始化app $controller = $app->make('MyController'); // 調用控制器 return $controller->callAction('index', $parameters); //調用控制其方法並傳參 });});