Laravel的路由命名空间

...

一般用法

1
2
3
Route::namespace('Admin')->group(function () {
// 在 "App\Http\Controllers\Admin" 命名空间下的控制器
});

进阶用法

如果我们的类不在App\Http\Controllers下, 要使用namespace, 就要修改一些东西
修改文件App\Providers\RouteServiceProvider

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* @var string
*/
protected $namespace = 'App\Http\Controllers';

protected $apiNamespace = 'App\Http';

......

protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('api')
->namespace($this->apiNamespace)
->group(base_path('routes/api.php'));
}

使用

1
2
3
4
5
6
7
8
Route::namespace('Api')->group(function () {

Route::post('/order/update', 'Order@receive')->name('api.order.receive');
Route::post('/order/delete', 'Order@delete')->name('api.order.delete');

Route::get('/fetch-waybill', 'FetchWaybill@getNotFetchTask')->name('api.fetch-waybill.get');
Route::post('fetch-waybill/callback', 'FetchWaybill@callback')->name('api.fetch-waybill.callback');
});