域名根目錄如何訪問多應用中的默認一個應用?多應用如何設置默認應用、控制器、方法?
目錄結(jié)構(gòu):
app
controller
User.php
admin
controller
User.php
app3
abc.vip.com/user/info 訪問 app/controller/User控制器的info方法
abc.vip.com/admin/user/info 訪問app/admin/controller/User控制器的info方法
目前只能這樣
如果要目錄結(jié)構(gòu)如下:
app
api(默認應用)
controller
User.php
admin
controller
User.php
app3
可以把自動路由插件拿來改一下:
http://wtbis.cn/plugin/17
簡單,你先安裝這個插件,然后把config\plugin\webman\auto-route\route.php替換為下面的代碼:
<?php
use Webman\Route;
// 已經(jīng)設置過路由的uri則忽略
$routes = Route::getRoutes();
$ignore_list = [];
foreach ($routes as $tmp_route) {
$ignore_list[$tmp_route->getPath()] = 0;
}
$suffix = config('app.controller_suffix', '');
$suffix_length = strlen($suffix);
// 遞歸遍歷目錄查找控制器自動設置路由
$dir_iterator = new \RecursiveDirectoryIterator(app_path());
$iterator = new \RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $file) {
// 忽略目錄和非php文件
if (is_dir($file) || $file->getExtension() != 'php') {
continue;
}
$file_path = str_replace('\\', '/',$file->getPathname());
// 文件路徑里不帶controller的文件忽略
if (strpos(strtolower($file_path), '/controller/') === false) {
continue;
}
// 只處理帶 controller_suffix 后綴的
if ($suffix_length && substr($file->getBaseName('.php'), -$suffix_length) !== $suffix) {
continue;
}
// 根據(jù)文件路徑計算uri
$uri_path = str_replace(['/controller/', '/Controller/'], '/', substr(substr($file_path, strlen(app_path())), 0, - (4 + $suffix_length)));
$uri_path = strtolower($uri_path);
$seg = explode('/', $uri_path);
$default_app = config('plugin.webman.auto-route.app.default_app');
$is_default_app = false;
if ($seg[1] == $default_app) {
$uri_path = str_replace($default_app . '/', '', $uri_path);
$is_default_app = true;
}
// 根據(jù)文件路徑是被類名
$class_name = str_replace('/', '\\',substr(substr($file_path, strlen(base_path())), 0, -4));
if (!class_exists($class_name)) {
echo "Class $class_name not found, skip route for it\n";
continue;
}
// 通過反射找到這個類的所有共有方法作為action
$class = new ReflectionClass($class_name);
$class_name = $class->name;
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);
$route = function ($uri, $cb) use ($ignore_list) {
if (isset($ignore_list[$uri])) {
return;
}
Route::any($uri, $cb);
if ($uri !== '') {
Route::any($uri . '/', $cb);
}
$lower_uri = strtolower($uri);
if ($lower_uri !== $uri) {
Route::any($lower_uri, $cb);
Route::any($lower_uri . '/', $cb);
}
};
// 設置路由
$is_default_controller = false;
if ($is_default_app && substr($class_name, -6) == '\Index') {
$is_default_controller = true;
}
foreach ($methods as $item) {
$action = $item->name;
if (in_array($action, ['__construct', '__destruct'])) {
continue;
}
// action為index時uri里末尾/index可以省略
if ($action === 'index') {
// controller也為index時可以uri里可以省略/index/index
if ($is_default_controller) {
$route('/', [$class_name, $action]);
}
if (substr($uri_path, -6) === '/index') {
$route(substr($uri_path, 0, -6), [$class_name, $action]);
}
$route($uri_path, [$class_name, $action]);
}
$route($uri_path.'/'.$action, [$class_name, $action]);
}
}
在config\plugin\webman\auto-route\app.php這個配置文件里面增加default_app(默認應用)
<?php
return [
'enable' => true,
'default_app' => 'api',
];
搞定
自動路由版本已更新
沒有安裝自動路由插件的直接安裝即可支持默認應用
composer require webman/auto-route
已安裝的直接更新一下
composer update webman/auto-route