imlinfly / webman-annotation

Webman plugin linfly/annotation
MIT License
10 stars 0 forks source link

控制器继承时,注解扫描解析会报错 #11

Open twosimple opened 1 year ago

twosimple commented 1 year ago

namespace app\common\controller;

use app\admin\library\Auth; use app\admin\library\traits\Curd; use LinFly\Annotation\Annotation\Inherit; use LinFly\Annotation\Route\GetRoute; use think\Model;

/**

namespace app\admin\controller;

use app\common\controller\Backend1; use LinFly\Annotation\Annotation\Inherit; use LinFly\Annotation\Route\Controller; use LinFly\Annotation\Route\GetRoute;

[

Controller(prefix: '/admin/test')

] class Test extends Backend1 {

[GetRoute]

public function index()
{
    return 'index';
}

}

- 报错信息

Press Ctrl+C to stop. Start success. [Process:webman] Start scan annotations... [Process:webman] Scan annotations completed, time: 0.08s ^CFastRoute\BadRouteException: Cannot register two routes matching "/add" for method "GET" in /Users/destiny/cool-admin/cool-admin-webman/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php:86 Stack trace:

0 /Users/destiny/cool-admin/cool-admin-webman/vendor/nikic/fast-route/src/DataGenerator/RegexBasedAbstract.php(30): FastRoute\DataGenerator\RegexBasedAbstract->addStaticRoute('GET', Array, Array)

1 /Users/destiny/cool-admin/cool-admin-webman/vendor/nikic/fast-route/src/RouteCollector.php(44): FastRoute\DataGenerator\RegexBasedAbstract->addRoute('GET', Array, Array)

2 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/webman-framework/src/Route.php(382): FastRoute\RouteCollector->addRoute(Array, '/add', Array)

3 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/webman-framework/src/Route.php(182): Webman\Route::addRoute(Array, '/add', Array)

4 /Users/destiny/cool-admin/cool-admin-webman/vendor/linfly/annotation/src/Handle/RouteAnnotationHandle.php(268): Webman\Route::add(Array, '/add', Array)

5 /Users/destiny/cool-admin/cool-admin-webman/vendor/linfly/annotation/src/Handle/RouteAnnotationHandle.php(223): LinFly\Annotation\Handle\RouteAnnotationHandle::addRoute('/add', Array)

6 /Users/destiny/cool-admin/cool-admin-webman/config/plugin/linfly/annotation/route.php(16): LinFly\Annotation\Handle\RouteAnnotationHandle::createRoute()

7 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/webman-framework/src/Route.php(424): require_once('/Users/destiny/...')

8 /Users/destiny/cool-admin/cool-admin-webman/vendor/nikic/fast-route/src/functions.php(25): Webman\Route::Webman{closure}(Object(FastRoute\RouteCollector))

9 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/webman-framework/src/Route.php(427): FastRoute\simpleDispatcher(Object(Closure))

10 /Users/destiny/cool-admin/cool-admin-webman/support/bootstrap.php(132): Webman\Route::load(Array)

11 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/webman-framework/src/support/App.php(104): require_once('/Users/destiny/...')

12 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/workerman/Worker.php(2494): support\App::support{closure}(Object(Workerman\Worker))

13 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/workerman/Worker.php(1633): Workerman\Worker->run()

14 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/workerman/Worker.php(1426): Workerman\Worker::forkOneWorkerForLinux(Object(Workerman\Worker))

15 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/workerman/Worker.php(1400): Workerman\Worker::forkWorkersForLinux()

16 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/workerman/Worker.php(560): Workerman\Worker::forkWorkers()

17 /Users/destiny/cool-admin/cool-admin-webman/vendor/workerman/webman-framework/src/support/App.php(131): Workerman\Worker::runAll()

18 /Users/destiny/cool-admin/cool-admin-webman/start.php(4): support\App::run()

19 {main}

Workerman[start.php] stopping ... worker[webman:97666] exit with status 64000 Workerman[start.php] has been stopped


大佬这种写法还能抢救一下吗,因为是写后台管理的,一些curd都是封装到基础控制器的
imlinfly commented 1 year ago

你好, PHP内置的注解扫描和doctrine/annotation都无法获取到父类的方法。 并不推荐这样使用注解路由,能否换种方法? 比如:在控制器引入trait类

CrudTrait

/**
 * CrudTrait
 */
trait CrudTrait
{
    #[GetRoute]
    public function add()
    {
        return 'test';
    }

    #[GetRoute]
    public function edit()
    {
        return 'test';
    }

    #[GetRoute]
    public function del()
    {
        return 'test';
    }
}

TestController

#[
    Controller(prefix: '/admin/test')
]
class Test extends Backend1
{
    use CrudTrait;

    #[GetRoute]
    public function index()
    {
        return 'index';
    }
}

经过测试这种方式是可以被扫描到的 image

twosimple commented 1 year ago

你好大佬,还有一个问题哈,那个注解验证能动态吗,比如curd 其实验证的方法都一样 只是验证的验证器不是一样,那个可以自动根据当前文件的命名空间解析出验证器的文件吗,如果这样不行的话,其实那个trait类就起不到简洁代码的作用了,因为没个控制器都要实现一遍curd,注解不通的验证器.

//验证数据
            if ($this->modelValidate) {
                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                if (class_exists($name)) {
                    $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                    validate($validate)->check($params);
                }
            }
            $result = $this->model->save($params);

这个代码是我之前封装curd trait里根据模型获取验证器,就是注解的时候能兼容下这个需求吗 可以根据当前的控制器命名空间

imlinfly commented 1 year ago

刚更新了支持自动识别注解验证器类 可以试试开发版 composer require linfly/annotation=1.x-dev

twosimple commented 1 year ago

大佬 牛逼 太感谢了