有时候在同一个工程里需要同时开发多个客户端接口的项目,例如移动端APP和web前端。
由于用户认证规则,签名校验等规则会不一样,这时候可以使用注册多个模块,每个模块单独的路由文件
示例如下
FooRouteServiceProvider
<?phpnamespace App\Modules\Foo;use App\Modules\Foo\Middleware\FooMiddleware;use Illuminate\Support\Facades\Route;use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;class FooRouteServiceProvider 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\Modules\Foo\Controller';/*** Define your route model bindings, pattern filters, etc.** @return void*/public function boot(){//parent::boot();}/*** Define the routes for the application.** @return void*/public function map(){$this->mapRoutes();}protected function mapRoutes(){Route::prefix('foo')->middleware([FooMiddleware::class,])->namespace($this->namespace)->group(base_path('app/Modules/Foo/route.php'));}}
FooServiceProvider
<?phpnamespace App\Modules\Foo;class FooServiceProvider extends \Illuminate\Support\ServiceProvider{/*** Bootstrap any application services.** @return void*/public function boot(){}/*** Register any application services.** @return void*/public function register(){//注册路由$this->app->register(FooRouteServiceProvider::class);}}
config/app.php中注册模块
//foo模块App\Modules\Foo\FooServiceProvider::class,
