需求
最近由於業務功能的需求,需要根據資料庫記錄的請求路徑(如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);