需求
最近由於業務功能的需求,需要根據數據庫記錄的請求路徑(如admin/auth/menu/46/edit)、請求方法(如GET)去匹配路由實例,拿到路由實例後續做一些其他事情。
分析
其實就是路由的核心功能(將一類請求映射匹配到一個回調類型的變量)。框架本身自帶的功能,查找源碼是以下代碼塊實現的:
// 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;
}
代碼大概邏輯是:遍歷路由集合,將當前請求丟給四個驗證器 UriValidator、MethodValidator、SchemeValidator、HostValidator 去逐一驗證匹配,驗證器主要去驗證匹配請求對象的 pathInfo、method、scheme、host 信息。
所以只需要修改當前的請求對象的這些信息,丟給上面的 matches 方法就可以實現我需要的功能了。
實現
由於是同一個項目,所以 scheme、host 與當前請求一致無需修改。而 pathInfo、method 是兩個私有屬性以及沒有找到對應寫權限的方法。所以實現一個有能力寫屬私性的宏方法即可。最終代碼如下:
<?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);
原文鏈接
- https://www.guanguans.cn