laravel 添加一个V1 接口模块 -api
laravel 添加一个V1 接口模块 -api
laravel 添加一个V1 接口模块 -api
app/Providers/RouteServiceProvider.php 中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public function map() { $this->mapApiRoutes();
$this->mapWebRoutes();
$this->mapTestRoutes(); }
protected function mapApiRoutes() //API 接口模块 { Route::prefix('api') ->middleware('api') ->namespace($this->namespace.'\\Api') ->group(base_path('routes/api.php')); }
public function mapTestRoutes() //V1接口模块 { Route::prefix('test') ->namespace($this->namespace.'\\V1') ->group(base_path('routes/test.php')); }
|
routes/test.php 路由
1
| Route::get('foo', 'FooController@index');
|
app/Http/Controllers/V1/FooController.php 模块
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?php namespace App\Http\Controllers\V1;
use App\Http\Controllers\Controller;
class FooController extends Controller { public function index() { echo "FooController foo"; } }
|
app/Http/Middleware/fei.php 自定义中间件 app/Http/Kernel.php 加载中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| <?php
namespace App\Http\Middleware; use Closure; class fei { public function handle($request, Closure $next) { exit('使用自定义文件中间件'); return $next($request); } }
protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, ], 'api' => [ 'throttle:60,1', 'bindings', ], 'fei'=>[ fei::class ] ];
|
