Laravel 路由改造篇 多个域名

Laravel改造完整版传送门

基础用法参考链接: https://learnku.com/docs/laravel/7.x/routing/7458

由于Laravel内置只有routes/api.phproutes/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.phpapi.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)));
    }
}

修改完毕~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值