基础用法参考链接: https://learnku.com/docs/laravel/7.x/routing/7458
由于Laravel内置只有routes/api.php
与routes/web.php
两个文件明显不够用,都写在一个文件太过拥挤不易维护,如果用include
好像有点low,所以进行了调整。
路由分层
添加目录
现在想要在routes
下添加新的目录一个通过api
访问,一个通过web
访问
目录结构调整
|--- app --- 应用目录
| | -- Http
| | | -- Controller -- 控制器目录
| | | | -- Api -- 外部通过接口域名访问目录(api.exp.com)
| | | | -- Web -- 外部通过网页域名访问目录(web.exp.com)
...
|--- routes --- 路由目录
| | -- api
| | | -- test1.php
| | | -- test2.php
| | | ...
| | -- web
| | | -- test1.php
| | | -- test2.php
| | | ...
| |-- console.php -- 控制台路由定义
| |-- web.php -- 网页路由定义
...
访问app/Http/Controller/Api/Xxx.php
通过 api.exp.com/xxx
访问app/Http/Controller/Web/Xxx.php
通过 web.exp.com/xxx
设置入口文件
由于要分多个域名,对应的访问多个入口文件入口文件在public
下
目录参考:https://blog.youkuaiyun.com/eight_eyes/article/details/110677780
修改index.php
添加常量ENTRY_FILE
<?php
define('LARAVEL_START', microtime(true));
require __DIR__.'/../vendor/autoload.php';
// 定义路由KEY - 用于在路由的地方进行隔离不允许在web下的访问api下
define('ENTRY_FILE', 'WEB');
复制index.php
为api.php
并修改ENTRY_FILE
<?php
define('LARAVEL_START', microtime(true));
require __DIR__.'/../vendor/autoload.php';
// 定义路由KEY - 用于在路由的地方进行隔离不允许在api下的访问web下
define('ENTRY_FILE', 'API');
设置路由请求
打开文件app/Providers/RouteServiceProvider.php
修改map
函数
原文件
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
$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()
{
Route::prefix('api')
->middleware('api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
调整后
<?php
namespace App\Providers;
use App\Enum\System\RouteEnum;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
/**
* Class RouteServiceProvider
*
* @package App\Providers
*/
class RouteServiceProvider extends ServiceProvider
{
// ...
/**
* Define the routes for the application.
*
* @return void
*/
public function map()
{
// 关注这里!!!
// 原来是调用了两个map routes/web.php与routes/api.php
// 现在要将其改造为调用routes/api与routes/web目录下所有路由文件
// 实现路由隔离
if (defined('ENTRY_FILE') && RouteEnum::isValid(ENTRY_FILE)) {
$this->autoloadDirMapRoutes(ENTRY_FILE);
}
// 留一个通用的访问
$this->mapIndexRoutes();
}
/**
* 通用路由
*/
protected function mapIndexRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/index.php'));
}
/**
* 自动加载目录路由
*
* @param string $dir
*
* @return void
*/
protected function autoloadDirMapRoutes(string $dir): void
{
$dir = strtolower($dir);
$routePath = scandir(base_path(sprintf('routes/%s', $dir)));
foreach ($routePath as $path) {
!in_array($path, ['.', '..']) && $this->setRoutes($path, $dir);
}
}
/**
* 设置路由
* 访问 域名/路由文件名/XXX
*
* @param string $path
* @param string $dir
*
* @return void
*/
protected function setRoutes(string $path, string $dir)
{
// 判断路由文件类型
if (substr($path, -4) !== '.php') {
return;
}
// 设置API路由 前缀为路由文件名
// middlerware对应app/Http/Kernel.php文件$middlewareGroups数组key
Route::prefix(substr($path, 0, -4))
->middleware($dir)
->namespace($this->namespace . '\\' . ucfirst($dir))
->group(base_path(sprintf('routes/%s/%s', $dir, $path)));
}
}
修改完毕~