自Laravel5.3之后web和api的路由是通过文件去区分,分别是routes下的api.php和web.php
如果把路由加到api.php中,在访问的时候需要加上实际的路由前加上/api/的前缀,例如:xx.com/api/user/save
根据问题,你可以看下app/Providers/RouteServiceProvider.php这个文件中的mapApiRoutes( )方法。
建议将xx.com/api/ 作为base URI,如果再次分路由模块可以使用路由中的group,不同的功能模块建立不同的group。
例如:
用户模块:
Route::prefix('user')->namespace('Api')->group(function () {
Route::any('index', 'UserController@index');
Route::any('login', 'UserController@login');
});
对应的访问方式就是:
xx.com/api/user/index
xx.com/api/user/login
文章模块:
Route::prefix('article')->namespace('Api')->group(function () {
Route::any('index', 'ArticleController@index');
Route::any('save', 'ArticleController@save');
});
对应的访问方式就是:
xx.com/api/article/index
xx.com/api/article/save
自Laravel5.3之后web和api的路由是通过文件去区分,分别是routes下的api.php和web.php
如果把路由加到api.php中,在访问的时候需要加上实际的路由前加上/api/的前缀,例如:xx.com/api/user/save
根据问题,你可以看下app/Providers/RouteServiceProvider.php这个文件中的mapApiRoutes( )方法。
建议将xx.com/api/ 作为base URI,如果再次分路由模块可以使用路由中的group,不同的功能模块建立不同的group。
例如:
用户模块:
Route::prefix('user')->namespace('Api')->group(function () {
Route::any('index', 'UserController@index');
Route::any('login', 'UserController@login');
});
对应的访问方式就是:
xx.com/api/user/index
xx.com/api/user/login
文章模块:
Route::prefix('article')->namespace('Api')->group(function () {
Route::any('index', 'ArticleController@index');
Route::any('save', 'ArticleController@save');
});
对应的访问方式就是:
xx.com/api/article/index
xx.com/api/article/save
推荐文章: