注释和注解区别
注释:给程序员看,帮助理解代码,对代码起到解释、说明的作用。
注解:给应用程序看,用于元数据的定义,单独使用时没有任何作用,需配合应用程序对其元数据进行利用才有作用。
通过注解定义路由
<?php declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Annotation\AutoController;
/**
* @AutoController()
*/
class IndexController extends AbstractController{
public function test(){
$msg = '这个是@AutoController注解演示';
return [
'msg'=>$msg
];
}
}
通过注解定义方法
<?php declare(strict_types=1);
namespace App\Controller;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
/**
* @Controller()
*/
class IndexController extends AbstractController{
/**
* @RequestMapping(path="test",methods="get,post")
*/
public function test(RequestInterface $request){
$name = $request->input('name','');
$msg = '这个是@Controller注解演示';
return [
'msg'=>$msg,
'name'=>$name
];
}
}
我们没有定义路由也可以正常访问
如果删除注解工,则不能访问,说明注解起作用的
转https://www.cnblogs.com/hu308830232/p/15240883.html
如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术
转载请注明:hyperf中注解的使用 出自老鄢博客 | 欢迎分享