我想方法小駝峰 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 協議》,轉載必須註明作者和本文連結