需求
最近由于业务功能的需求,需要根据数据库记录的请求路径(如admin/auth/menu/46/edit )、请求方法(如GET )去匹配路由实例,拿到路由实例后续做一些其他事情。
分析
其实就是路由的核心功能(将一类请求映射匹配到一个回调类型的变量)。框架本身自带的功能,查找源码是以下代码块实现的:
public function match(Request $request)
{
$routes = $this->get($request->getMethod());
$route = $this->matchAgainstRoutes($routes, $request);
return $this->handleMatchedRoute($request, $route);
}
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) {
return $route->matches($request, $includingMethod);
});
}
public function matches(Request $request, $includingMethod = true)
{
$this->compileRoute();
foreach ($this->getValidators() as $validator) {
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){
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){
$routeCollection = app(Router::class)->getRoutes();
$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){
return $route->matches($this, $includingMethod);
});
};
}
}
注册请求宏
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);
原文链接
|