Hyperf从入门到精通(2)
简述
Hyperf 从入门到精通第二讲,讲述 HTTP 路由的定义及使用,包括配置文件定义路由和注解定义路由,同时引入注解的概念并讲述注释和注解的区别,以及注解的基本实现流程。
路由定义
通过配置文件定义路由
Router::get("/hello-hyperf", function () {
return 'Hello,Hyperf';
});
Router::get("/hello-hyperf","App\Controller\IndexController::hello");
Router::get("/hello-hyperf","App\Controller\IndexController@hello");
Router::get("/hello",[\App\Controller\IndexController::class,'hello']);
Router::post("/hello-hyperf","App\Controller\IndexController::hello");
Router::post("/hello-hyperf","App\Controller\IndexController@hello");
Router::post("/hello",[\App\Controller\IndexController::class,'hello']);
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController::hello');
Router::addRoute(['GET', 'POST', 'HEAD'], '/', 'App\Controller\IndexController@hello');
Router::addRoute(['GET', 'POST', 'HEAD'], '/', [\App\Controller\IndexController::class,'hello']);
通过注解定义路由
@AutoController注解
@Controller注解
@Controller注解需与@RequestMapping,@GetMapping,@PostMapping等注解联合使用
测试
curl -w "response_code:%{http_code}" http://127.0.0.1:9501/hello-hyperf