hyperf 路由注解 方法小驼峰 url 蛇形实现
我想方法小驼峰 url蛇形
例如:
访问video_list
, 调用videoList
方法
如果用路由文件很简单,自动注解不行。
经提醒
先创建中间件
重写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 协议》,转载必须注明作者和本文链接