分清各个域名的职责
- admin 后台
- api 前端接口
- web 默认首页
- 路由文件如下,每个文件,不同的领域
新增一个路由服务提供者 app/provider/RouteServiceProvider
代码如下:
<?php
namespace App\Providers;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
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';
/**
* 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->initMap();
}
protected function initMap()
{
$this->mapAdminRoutes();
$this->mapApiRoutes();
$this->mapWebRoutes();
}
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
}
/**
* Define the "api" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapApiRoutes()
{
$apiConfig = config('app.route.api');
Route::domain($apiConfig['domain'].'.'.$this->getBaseDomain())
->prefix($apiConfig['prefix'])
->namespace($apiConfig['namespace'])
->group(base_path('routes/api.php'));
}
/**
* Define the "admin" routes for the application.
*
* These routes are typically stateless.
*
* @return void
*/
protected function mapAdminRoutes()
{
// Admin::registerAuthRoutes();
//后台路径
$domain = config('admin.route.domain').'.'.$this->getBaseDomain();
Route::domain($domain)
->prefix(config('admin.route.prefix'))
->namespace(config('admin.route.namespace'))
->middleware(config('admin.route.middleware'))
->group(base_path('routes/admin.php'));
//后台登录权限路径
Route::domain($domain)
->prefix(config('admin.route.prefix'))
->middleware(config('admin.route.middleware'))
->group(base_path('routes/adminAuth.php'));
}
//获取一级域名
protected function getBaseDomain()
{
return config('app.base_domain');
}
}
- 注册服务提供者
最后在 config/app.php插入上面的路由服务提供者类名
'providers' => \App\Providers\RouteServiceProvider