Skip to content

Commit

Permalink
自动识别路由到类的layer并支持自动中间件
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Nov 26, 2024
1 parent 4ab9f6b commit 7b0a3d0
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/think/route/dispatch/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,26 @@ public function exec()
[$class, $action] = $this->dispatch;

// 设置当前请求的控制器、操作
$controllerLayer = $this->rule->config('controller_layer') ?: 'controller';
[$layer, $controller] = explode('/' . $controllerLayer . '/', trim(str_replace('\\', '/', $class), '/'));
$controllerLayer = $this->rule->config('controller_layer') ?: 'controller';
if (str_contains($class, '\\' . $controllerLayer . '\\')) {
[$layer, $controller] = explode('/' . $controllerLayer . '/', trim(str_replace('\\', '/', $class), '/'));
$layer = trim(str_replace('app', '', $layer), '/');
} else {
$layer = '';
$controller = trim(str_replace('\\', '/', $class), '/');
}

if ($layer && !empty($this->option['auto_middleware'])) {
// 自动为顶层layer注册中间件
$alias = $this->app->config->get('middleware.alias', []);

if (isset($alias[$layer])) {
$this->app->middleware->add($layer, 'route');
}
}

$this->request
->setLayer(trim(str_replace('app', '', $layer), '/'))
->setLayer($layer)
->setController($controller)
->setAction($action);

Expand Down

0 comments on commit 7b0a3d0

Please sign in to comment.