hyperf 路由注解 方法小驼峰 url 蛇形实现

我想方法小驼峰 url蛇形

例如:
访问video_list, 调用videoList方法

如果用路由文件很简单,自动注解不行。

经提醒

hyperf注解路由 方法骆驼 路由蛇形实现

先创建中间件

https://doc.hyperf.io/#/zh-cn/middleware/middleware?id=%e8%87%aa%e5%ae%9a%e4%b9%89-coremiddleware-%e7%9a%84%e8%a1%8c%e4%b8%ba

重写dispatch

class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{



    public function dispatch(ServerRequestInterface $request): ServerRequestInterface
    {

        $path = $request->getUri()->getPath();
        $path_arr = explode('_',$path);  //分割路径

        $path_res = '';
        foreach ($path_arr as $key => $path_str)
            if( $key)   //如果不是第一个字符串
                $path_res .= ucfirst($path_str);
            else
                $path_res .= $path_str;

        $routes = $this->dispatcher->dispatch($request->getMethod(), $path_res);
        $dispatched = new Dispatched($routes);
        return Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, $dispatched));
    }

}
本作品采用《CC 协议》,转载必须注明作者和本文链接
专心学习不瞎搞
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
87
粉丝
105
喜欢
480
收藏
717
排名:109
访问:8.7 万
私信
所有博文
社区赞助商