Skip to content

Commit 8cdc77a

Browse files
committed
update
1 parent 5a8f525 commit 8cdc77a

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Handler extends ExceptionHandler
3131
use ApiException;
3232
}
3333
```
34-
> 客户端访问的时候,注意要在头信息中增加 Accept: application/json 或 Accept: application/vnd.api+json,这样返回错误信息的时候才是JSON格式
34+
3535
## 4.身份认证
3636

3737
修改User模型

src/Api/ApiServiceProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class ApiServiceProvider extends ServiceProvider
2121
* @var array
2222
*/
2323
protected $routeMiddleware = [
24+
'api.accept' => Middleware\AcceptHeader::class,
2425
'token.auth' => Middleware\TokenAuthRefresh::class,
2526
'token.guess' => Middleware\TokenGuess::class,
2627
'client.check' => Middleware\ClientCheck::class,
@@ -42,7 +43,12 @@ public function boot()
4243
}
4344

4445
if (file_exists($routes = $this->getRouteFile())) {
45-
$this->loadRoutesFrom($routes);
46+
Route::as(config('api.route.as'))
47+
->domain(config('api.route.domain'))
48+
->middleware(config('api.route.middleware'))
49+
->namespace(config('api.route.namespace'))
50+
->prefix(config('api.route.prefix'))
51+
->group($routes);
4652
}
4753
}
4854

src/Api/Console/stubs/routes.stub

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
<?php
22

33
use Illuminate\Routing\Router;
4+
use Illuminate\Support\Facades\Route;
45

56
Route::group([
6-
'domain' => config('api.route.domain'),
7-
'prefix' => config('api.route.prefix'),
8-
'namespace' => config('api.route.namespace'),
9-
'middleware' => config('api.route.middleware'),
7+
// 'as' => '',
8+
// 'domain' => '',
9+
// 'prefix' => '',
10+
// 'namespace' => '',
11+
// 'middleware' => '',
1012
], function (Router $router) {
11-
1213
$router->get('/', 'IndexController@index');
13-
1414
});
15+
16+
// foreach (glob(app_path('Api/Routes') . '/*.php') as $routeFile) {
17+
// require $routeFile;
18+
// }

src/config/api.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,33 @@
1212

1313
'route' => [
1414
/**
15-
* 可配置接口独立域名
15+
* API 路由命名前缀
1616
*/
17-
'domain' => env('API_ROUTE_DOMAIN', ''),
17+
'as' => 'api.',
1818
/**
19-
* 不实用独立域名,接口地址前缀
19+
* 可配置 API 独立域名
2020
*/
21-
'prefix' => env('API_ROUTE_PREFIX', 'api'),
21+
'domain' => env('API_ROUTE_DOMAIN', ''),
2222
/**
23-
* 接口控制器命名空间
23+
* 不使用用独立域名,API 地址前缀
2424
*/
25-
'namespace' => 'App\\Api\\Controllers',
25+
'prefix' => env('API_ROUTE_PREFIX', 'api'),
26+
/**
27+
* API 控制器命名空间
28+
*/
29+
'namespace' => 'App\\Api\\Controllers',
2630
/**
2731
* 中间件
2832
*/
29-
'middleware' => ['api'],
33+
'middleware' => ['api', 'api.accept'],
3034
/**
3135
* 身份认证的中间件
3236
*/
33-
'middleware_auth' => ['api', 'token.auth'],
37+
'middleware_auth' => ['api', 'api.accept', 'token.auth'],
3438
/**
3539
* 获取token,获取不到也不报错的中间件
3640
*/
37-
'middleware_guess' => ['api', 'token.guess'],
41+
'middleware_guess' => ['api', 'api.accept', 'token.guess'],
3842
],
3943

4044
/**

0 commit comments

Comments
 (0)