@walkor
邀請(qǐng)walkor大佬看看,這個(gè)路由方案能不能行
情況是這樣:前端訪問(wèn)接口的方式是地址欄path+api
前端因?yàn)槭谴虬玫模琍HP開發(fā)者基本上不需要管前端。
這個(gè)時(shí)候PHP開發(fā)者在后端更改了admin入口之后,前端的接口地址會(huì)自動(dòng)變更為PHP開發(fā)者定義的admin入口,那么在這種情況下admin的所有地址都需要變更為自定義的入口。
以下這個(gè)方案實(shí)現(xiàn)了控制器和中間件調(diào)用,對(duì)于webman框架來(lái)說(shuō),還有很多功能和問(wèn)題。
需要的幫助:能不能實(shí)現(xiàn)一下自定義應(yīng)用名映射或者幫忙指點(diǎn)一下問(wèn)題
webman-admin的后臺(tái)路徑如何更改?
webman-admin修改默認(rèn)路由
以上這兩種方案均只能更改Index/index,并不能全部替換
<?php
/**
* 自定義Admin入口
* @authors 余心(RenLoong)
* @home https://github.com/RenLoong/loong-admin
*/
use app\expose\utils\Str;
use support\Log;
use Webman\Route;
$admin_path = getenv('SERVER_ADMIN_PATH');
if ($admin_path && $admin_path != 'admin') {
Route::any('/admin[{path:.+}]', function () {
$request = request();
$error = "ADMIN ROUTE:{$request->getRealIp()} {$request->method()} {$request->host(true)}{$request->uri()}";
Log::error($error);
return not_found();
});
Route::any('/' . $admin_path . '[{path:.+}]', function () {
$args = func_get_args();
$request = $args[0];
$pathArr = explode('/', $request->path());
// Str::title(),轉(zhuǎn)為首字母大寫的標(biāo)題格式
$controller = Str::title(empty($pathArr[2]) ? 'Index' : $pathArr[2]);
$action = empty($pathArr[3]) ? 'index' : $pathArr[3];
$controller = '\\app\\admin\\controller\\' . $controller . 'Controller';
if (!class_exists($controller)) {
$error = "ADMIN ROUTE:{$request->getRealIp()} {$request->method()} {$request->host(true)}{$request->uri()}";
Log::error($error);
return not_found();
}
$request->app = 'admin';
$request->controller = $controller;
$request->action = $action;
$config_middlewares = config('middleware');
$common_middlewares = isset($config_middlewares['']) ? $config_middlewares[''] : [];
$admin_middlewares = isset($config_middlewares['admin']) ? $config_middlewares['admin'] : [];
$middlewares = array_merge($common_middlewares, $admin_middlewares);
// 最終的控制器處理邏輯(核心 handler)
$coreHandler = function () use ($controller, $action, $args) {
return call_user_func_array([new $controller, $action], $args);
};
// 構(gòu)建中間件洋蔥圈
$dispatcher = array_reduce(
array_reverse($middlewares),
function ($next, $middleware) {
return function ($request) use ($middleware, $next) {
$middleware = new $middleware;
return $middleware->process($request, $next);
};
},
$coreHandler // 最終業(yè)務(wù)處理
);
// 執(zhí)行并返回響應(yīng)
return $dispatcher($request);
});
}
也可以在 config/route.php 中掃描所有controller文件,找到所有控制器,每個(gè)控制器添加一條路由,改變控制器的路徑。
大佬,經(jīng)過(guò)你的指點(diǎn)改寫成這樣
<?php
/**
* 自定義Admin入口
* @authors 余心(RenLoong)
* @home https://github.com/RenLoong/loong-admin
*/
use Webman\Route;
$admin_path = getenv('SERVER_ADMIN_PATH');
if ($admin_path && $admin_path != 'admin') {
Route::disableDefaultRoute('', 'admin');
$controllersClass = glob(app_path('admin') . '/controller/*Controller.php');
$len = count($controllersClass);
$routes = [];
for ($i = 0; $i < $len; $i++) {
$value = $controllersClass[$i];
$controllerName = str_replace('Controller', '', basename($value, '.php'));
$classStr = 'app' . str_replace([app_path(), '.php', '/'], ['', '', '\\'], $value);
$reflection = new \ReflectionClass('\\' . $classStr);
// 忽略抽象類、接口
if ($reflection->isAbstract() || $reflection->isInterface()) {
continue;
}
$actions = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$construct = $reflection->getConstructor();
$actionsLen = count($actions);
for ($n = 0; $n < $actionsLen; $n++) {
$class = $actions[$n]->class;
$name = $actions[$n]->name;
if ($class === $classStr && !str_starts_with($name, '__')) {
$routes["/{$controllerName}/{$name}"] = [$class, $name];
}
}
}
$controllersVersionClass = glob(app_path('admin') . '/controller/**/*Controller.php');
if (!empty($controllersVersionClass)) {
$len = count($controllersVersionClass);
for ($i = 0; $i < $len; $i++) {
$value = $controllersVersionClass[$i];
$version=basename(dirname($value));
$controllerName = str_replace('Controller', '', basename($value, '.php'));
$classStr = 'app' . str_replace([app_path(), '.php', '/'], ['', '', '\\'], $value);
$reflection = new \ReflectionClass('\\' . $classStr);
// 忽略抽象類、接口
if ($reflection->isAbstract() || $reflection->isInterface()) {
continue;
}
$actions = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);
$construct = $reflection->getConstructor();
$actionsLen = count($actions);
for ($n = 0; $n < $actionsLen; $n++) {
$class = $actions[$n]->class;
$name = $actions[$n]->name;
if ($class === $classStr && !str_starts_with($name, '__')) {
$routes["/{$version}/{$controllerName}/{$name}"] = [$class, $name];
}
}
}
}
Route::group('/' . $admin_path, function ()use($routes) {
foreach ($routes as $key => $value) {
Route::any($key,$value);
}
});
}