多应用如何设置默认的应用、控制器和方法?

小龙

域名根目录如何访问多应用中的默认一个应用?多应用如何设置默认应用、控制器、方法?

1109 3 1
3个回答

six

webman不区分域名啊,所以根域名访问某个应用能访问到啊

ontheway

目录结构:

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方法

目前只能这样

如果要目录结构如下:

app
  api(默认应用)
    controller
      User.php
  admin
    controller
      User.php
  app3

可以把自动路由插件拿来改一下:
https://www.workerman.net/plugin/17

  • 小龙 2022-07-21

    对,目前就是想采用第二种目录结构,比较清晰,但还不知道该如何改

  • ontheway 2022-07-21

    简单,你先安装这个插件,然后把config\plugin\webman\auto-route\route.php替换为下面的代码:

    <?php
    
    use Webman\Route;
    
    // 已经设置过路由的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;
        }
    
        // 根据文件路径计算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;
        }
    
        // 根据文件路径是被类名
        $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',
    ];

    搞定

  • ontheway 2022-07-21

    还差一步,把默认路由关闭了,在config/route.php里面增加一行:

    Route::disableDefaultRoute();
  • 小龙 2022-07-22

    好的,谢谢

ontheway

自动路由版本已更新

没有安装自动路由插件的直接安装即可支持默认应用

composer require webman/auto-route

已安装的直接更新一下

composer update webman/auto-route
  • 暂无评论
年代过于久远,无法发表回答
🔝