laravel 中根據(jù)請求路徑、方法匹配路由實例

需求

最近由于業(yè)務功能的需求,需要根據(jù)數(shù)據(jù)庫記錄的請求路徑(如admin/auth/menu/46/edit)、請求方法(如GET)去匹配路由實例,拿到路由實例后續(xù)做一些其他事情。

分析

其實就是路由的核心功能(將一類請求映射匹配到一個回調(diào)類型的變量)??蚣鼙旧碜詭У墓δ?,查找源碼是以下代碼塊實現(xiàn)的:

// Illuminate/Routing/RouteCollection.php
public function match(Request $request)
{
    // 1. 獲取路由集合
    $routes = $this->get($request->getMethod());
    
    // 2. 匹配路由
    $route = $this->matchAgainstRoutes($routes, $request);

    return $this->handleMatchedRoute($request, $route);
}

// Illuminate/Routing/AbstractRouteCollection.php
protected function matchAgainstRoutes(array $routes, $request, $includingMethod = true)
{
    [$fallbacks, $routes] = collect($routes)->partition(function ($route) {
        return $route->isFallback;
    });

    return $routes->merge($fallbacks)->first(function (Route $route) use ($request, $includingMethod) {
        // 3. 遍歷匹配
        return $route->matches($request, $includingMethod);
    });
}

// Illuminate/Routing/Route.php
public function matches(Request $request, $includingMethod = true)
{
    $this->compileRoute();

    foreach ($this->getValidators() as $validator) {
        // 4. 遍歷驗證器驗證匹配
        if (! $includingMethod && $validator instanceof MethodValidator) {
            continue;
        }

        if (! $validator->matches($this, $request)) {
            return false;
        }
    }

    return true;
}

代碼大概邏輯是:遍歷路由集合,將當前請求丟給四個驗證器 UriValidatorMethodValidator、SchemeValidatorHostValidator 去逐一驗證匹配,驗證器主要去驗證匹配請求對象的 pathInfomethod、scheme、host 信息。

所以只需要修改當前的請求對象的這些信息,丟給上面的 matches 方法就可以實現(xiàn)我需要的功能了。

實現(xiàn)

由于是同一個項目,所以 schemehost 與當前請求一致無需修改。而 pathInfo、method 是兩個私有屬性以及沒有找到對應寫權限的方法。所以實現(xiàn)一個有能力寫屬私性的宏方法即可。最終代碼如下:

<?php

namespace App\Support\Macros;

use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use InvalidArgumentException;

class RequestMacro
{
    /**
     * 修改屬性
     */
    public function propertyAware(): callable
    {
        return function ($property, $value){
            /** @var \Illuminate\Http\Request $this */
            if (! property_exists($this, $property)) {
                throw new InvalidArgumentException('The property not exists.');
            }
            $this->{$property} = $value;

            return $this;
        };
    }
    
     /**
     * 匹配路由
     */
    public function matchRoute(): callable
    {
        return function ($includingMethod = true){
             // 1. 獲取路由集合
            /* @var \Illuminate\Routing\RouteCollection $routeCollection */
            $routeCollection = app(Router::class)->getRoutes();
            /** @var \Illuminate\Http\Request $this */
            $routes = is_null($this->method())
                ? $routeCollection->getRoutes()
                : Arr::get($routeCollection->getRoutesByMethod(), $this->method(), []);
            [$fallbacks, $routes] = collect($routes)->partition(function ($route){
                return $route->isFallback;
            });

            return $routes->merge($fallbacks)->first(function (Route $route) use ($includingMethod){
                // 2. 遍歷匹配
                return $route->matches($this, $includingMethod);
            });
        };
    }
}

注冊請求宏

// App\Providers\AppServiceProvider
public function register()
{
    Request::mixin($this->app->make(RequestMacro::class));
}

使用示例

$route = request()
    ->propertyAware('pathInfo', \Illuminate\Support\Str::start('admin/auth/menu/46/edit', '/'))
    ->propertyAware('method', 'GET')
    ->matchRoute();

dump($route);
usage.png

原文鏈接

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容