php注解使用示例
<?php
class indexController
{
#[route('/controller/index','get')]
public function index():void
{
echo "This is attribute index controller \r\n";
} #[route('/controller/test','get')]
public function test():void
{
echo "This is attribute test controller \r\n";
}
}
<?php
class workController
{
#[route('/controller/work','post')]
public function work():void
{
echo "This is attribute work controller \r\n";
}
}
<?php
#[Attribute(Attribute::IS_REPEATABLE|Attribute::TARGET_METHOD)]
class route{ public static $all = []; public static $path = ''; public static $method = 'GET'; public static $function = ''; public static $controller = ''; public function __construct(){} public function setPath(string $path):self
{
$this->path = $path;
return $this;
} public function setMethod(string $method):self
{
$this->method = $method;
return $this;
} public function setFunction(string $function):self
{
$this->function = $function;
return $this;
} public function setController(string $controller):self
{
$this->controller = $controller;
return $this;
} public function addRoute():void
{
self::$all[str_replace("Controller","",$this->controller)][$this->function] = $this;
}
<?php
public static function setRoute($controllerClass){ $ref = new ReflectionClass($controllerClass);
$controller = $ref->getName();
$methods = $ref->getMethods(); foreach($methods as $method){
$function = $method->getName();
$attributes = $method->getAttributes(route::class); foreach($attributes as $attribute){ $route = $attribute->newInstance();
// 拿到注解上的参数
$params = $attribute->getArguments(); $route->setController($controller)
->setFunction($function)
->setPath($params[0])
->setMethod($params[1])
->addRoute();
}
}
}
}
<?php
class app{
public function appInit():self
{
if(glob("./controller/*.php")){
foreach(glob("./controller/*.php") as $fileName){
require_once($fileName);
$className = str_replace("./controller/","",str_replace(".php","",$fileName));
route::setRoute($className);
//route::$all;exit();
}
} return $this;
} public function run($config){
$controller = $_GET['c']??$config['default_controller'];
$action = $_GET['a']??$config['default_function']; if(isset(route::$all[$controller][$action])){
$route = route::$all[$controller][$action];
$className = $route->controller;
$function = $route->function;
(new $className)->$function();
}else{
exit("404 Not Found!");
}
}
}
<?php
$config = array(
'default_controller'=>'index',
'default_function'=>'index',
); return $config;
<?php
#[Attribute(Attribute::IS_REPEATABLE|Attribute::TARGET_METHOD)]
class route{ public static $all = []; public static $path = ''; public static $method = 'GET'; public static $function = ''; public static $controller = ''; public function __construct(){} public function setPath(string $path):self
{
$this->path = $path;
return $this;
} public function setMethod(string $method):self
{
$this->method = $method;
return $this;
} public function setFunction(string $function):self
{
$this->function = $function;
return $this;
} public function setController(string $controller):self
{
$this->controller = $controller;
return $this;
} public function addRoute():void
{
self::$all[str_replace("Controller","",$this->controller)][$this->function] = $this;
} public static function setRoute($controllerClass){ $ref = new ReflectionClass($controllerClass); $controller = $ref->getName(); $methods = $ref->getMethods(); foreach($methods as $method){
$function = $method->getName();
$attributes = $method->getAttributes(route::class); foreach($attributes as $attribute){ $route = $attribute->newInstance();
// 拿到注解上的参数
$params = $attribute->getArguments(); $route->setController($controller)
->setFunction($function)
->setPath($params[0])
->setMethod($params[1])
->addRoute();
}
}
}
}
<?php
class app{
public function appInit():self
{
if(glob("./controller/*.php")){
foreach(glob("./controller/*.php") as $fileName){
require_once($fileName);
$className = str_replace("./controller/","",str_replace(".php","",$fileName));
route::setRoute($className);
//route::$all;exit();
}
} return $this;
} public function run($config){
$controller = $_GET['c']??$config['default_controller'];
$action = $_GET['a']??$config['default_function']; if(isset(route::$all[$controller][$action])){
$route = route::$all[$controller][$action];
$className = $route->controller;
$function = $route->function;
(new $className)->$function();
}else{
exit("404 Not Found!");
}
}
}
<?php
class indexController
{
#[route('/controller/index','get')]
public function index():void
{
echo "This is attribute index controller \r\n";
} #[route('/controller/test','get')]
public function test():void
{
echo "This is attribute test controller \r\n";
}
}
<?php
class workController
{
#[route('/controller/work','post')]
public function work():void
{
echo "This is attribute work controller \r\n";
}
}
<?php
$config = array(
'default_controller'=>'index',
'default_function'=>'index',
); return $config;
<?php
require_once('./config.php');
require_once('./route.php');
require_once('./init.php'); (new app)->appInit()->run($config);
php注解使用示例的更多相关文章
- MyBatis3-基于注解的示例
在基于注解的示例中,可以简化编写XML的过程,全部采用注解方式进行编写,并在注解上写SQL语句,语句和XML的语句保持一致,并且可以省略掉XML文件不用引入的好处.但还有一点,基于注解的方式还没有百分 ...
- MyBatis多参数传递之注解方式示例--转
原文地址:http://legend2011.blog.51cto.com/3018495/1015003 若映射器中的方法只有一个参数,则在对应的SQL语句中,可以采用#{参数名}的方式来引用此参数 ...
- spring注解controller示例
依赖库 spring 3.0 配置web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <w ...
- spring mvc 注解入门示例
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- Hibernate注解开发示例
-------------------------------------------------------------------customer------------------------- ...
- Spring 注解学习 详细代码示例
学习Sping注解,编写示例,最终整理成文章.如有错误,请指出. 该文章主要是针对新手的简单使用示例,讲述如何使用该注释,没有过多的原理解析. 已整理的注解请看右侧目录.写的示例代码也会在结尾附出. ...
- Spring bean依赖注入、bean的装配及相关注解
依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { priv ...
- 基于注解的SpringMVC
相比传统的继承Controller体系中某些类的方式,SpringMVC的注解具有以下优点: 1.Controller不再需要继承某个特定类,只是简单的POJO. 2.请求映射的配置非常方便灵活. 3 ...
- 【Java EE 学习 49 下】【Spring学习第一天】【MVC】【注解回顾】
一.MVC 1.使用Spring有一个非常大的好处,那就是能够实现完全面向接口编程,传统的使用Dao.Service并不能实现完全的面向接口编程. 2.示例:https://github.com/kd ...
随机推荐
- 【NOI P模拟赛】校门外歪脖树上的鸽子(树链剖分)
题面 2 ≤ n ≤ 2 × 1 0 5 , 1 ≤ m ≤ 2 × 1 0 5 , 1 ≤ l ≤ r ≤ n , 1 ≤ d ≤ 1 0 8 2 ≤ n ≤ 2 × 10^5,1 ≤ m ≤ 2 ...
- 并发编程Bug起源:可见性、有序性和原子性问题
以前古老的DOS操作系统,是单进行的系统.系统每次只能做一件事情,完成了一个任务才能继续下一个任务.每次只能做一件事情,比如在听歌的时候不能打开网页.所有的任务操作都按照串行的方式依次执行. 这类服务 ...
- Linux之如何配置IPV6网络
配置IPV6地址小笔记 #例题: 1)为server添加一个IPv6地址fd00:ba5e:ba11:10::10/64: 2)为client添加一个IPv6地址fd00:ba5e:ba11:10:: ...
- Python中的super函数,你熟吗?
摘要:经常有朋友问,学 Python 面向对象时,翻阅别人代码,会发现一个 super() 函数,那这个函数的作用到底是什么? 本文分享自华为云社区<Python中的super函数怎么学,怎么解 ...
- Netty内存池的整体架构
一.为什么要实现内存管理? Netty 作为底层网络通信框架,网络IO读写必定是非常频繁的操作,考虑到更高效的网络传输性能,堆外内存DirectByteBuffer必然是最合适的选择.堆外内存在 JV ...
- K8s nginx-ingress 如何配置二级目录转发远程静态服务器基于Vue路由history模式打包的应用程序
背景 首先这标题有点绕,我先解释下: 首先我们有静态服务器,上面某个目录有Vue路由history模式打包的应用程序(也就是build后的产物): 但是静态服务器一般不做对外域名用的,我们需要在k8s ...
- 分布式文件存储 CephFS的应用场景
块存储 (适合单客户端使用) 典型设备:磁盘阵列,硬盘. 使用场景: a. docker容器.虚拟机远程挂载磁盘存储分配. b. 日志存储. 文件存储 (适合多客户端有目录结构) 典型设备:FTP.N ...
- 基于Alpine镜像定制自己的工具箱
Alpine介绍 Alpine 操作系统是一个面向安全的轻型 Linux 发行版.目前 Docker 官方已开始推荐使用 Alpine 替代之前的 Ubuntu 做为基础镜像环境.这样会带来多个好处. ...
- mac 批量修改文件的后缀名
1-将需要修改的文件拖到同一个文件夹 2-打开终端输入 for i in *; do mv "$i" "$i.jpg";done
- MES与工业互联网的关联边界在哪里?
MES和工业互联网本不存在关联边界,如果有,那也只是MES包括在工业互联网中,是工业互联网的一部分.二者本来就是处于两个不同维度提出来的概念.MES是从信息系统维度提出来的,上承ERP.下接PCS的制 ...