请问一个多级目录下路由的问题

changup

webman访问路径

http://server.com/app/b/c/index/hello

期望的访问路径

http://server.com/app/b.c.index/hello
这个怎么配置路由啊,谢谢大佬

885 3 0
3个回答

taozywu

目录结构如下:

app
 --test
    --test2
        --controller
            Index.php
<?php

namespace app\test\test2\controller;

use support\Request;

class Index
{

    public function hello(Request $request)
    {
        return response('ok');
    }
}

路由配置:

config/route.php
Route::any('/app/test.test2.index/hello', [app\test\test2\controller\Index::class, 'hello']);
  • changup 2022-09-16

    谢谢大佬,有没有通用的处理办法,不用挨个写这么多

taozywu
Route::any('/app/{name1}/{name2}', function ($request, $name1 = null, $name2 = null) {
    // @todo需要优化
    list($app1, $app2, $controller) = explode(".", $name1);
    $class_name = "app\\{$app1}\\{$app2}\\controller\\" . ucwords($controller).config("app.controller_suffix", "");
    $class = new $class_name;
    return call_user_func([$class, $name2], $request);
 });

===
仅做参考

taozywu

使用nginx代理的话,可以使用rewrite

upstream webman {
    server 127.0.0.1:8787;
    keepalive 10240;
}

location / {
    rewrite ^/app/\.(.*)\.(.*)\.(.*)/(.*)$ $1/$2/$3/$4 break;
    proxy_pass   http://webman;
}

===
仅做参考

年代过于久远,无法发表回答
×
🔝