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 ...
随机推荐
- Excel 逻辑函数(一):IF 和 IFS
IF IF 函数有三个参数,第一个为条件判断,第二个是当条件为真时执行的表达式,第三个是条件为假时执行的表达式. IF(A1="是", A2 * 0.8, 0),如果 A1 单元格 ...
- AI听曲识歌!哼曲、口哨吹,都能秒识! ⛵
作者:韩信子@ShowMeAI 深度学习实战系列:https://www.showmeai.tech/tutorials/42 自然语言处理实战系列:https://www.showmeai.tech ...
- 记一次用arthas排查jvm中CPU占用过高问题
记一次使用arthas排查jvm中CPU占用过高问题.这工具屌爆了 碾压我目前使用的全部JVM工具. 安装 小试 curl -O https://arthas.aliyun.com/arthas-bo ...
- 记一次python + selenium小项目出现的问题与解决办法
记一次python + selenium小项目出现的问题与解决办法 如何接入代理 def crawl_xdaili(self):#代理 可不用 需要时 解除注释 """ ...
- VM虚拟机安装
VM虚拟机安装 1.安装vm虚拟机软件 1.1 双击打开虚拟机文件 1.2 根据向导安装 下一步 安装好了 不要着急点完成在 安装目录中有许可证. 1.3激活操作 2.虚拟机原理简介 3. 新建虚拟机 ...
- Android平台Camera2数据如何对接RTMP推流到服务器
1. Camera2架构 在Google 推出Android 5.0的时候, Android Camera API 版本升级到了API2(android.hardware.camera2), 之前使用 ...
- 高可用代理服务器实现keepalive+squid
〇.前言 之前单机部署了squid代理服务器,现在实现一下高可用. 还有自定义squid的error页面 准备:两台centos7(1C2GB) 三个可用IP,一主一备一虚拟IP(VIP) 一.安 ...
- opencv videocapture
import time import cv2 import numpy as np from os import path import pickle ''' 关于camera id 此处需要稍微说几 ...
- day04-1群聊功能
多用户即时通讯系统04 4.编码实现03 4.5功能实现-群聊功能实现 4.5.1思路分析 群聊的实现思路和私聊的实现非常类似. 不同的是:私聊时,服务端接收到消息后,只需要找出接收方的socket并 ...
- ELK基于ElastAlert实现日志的微信报警
文章转载自:https://mp.weixin.qq.com/s/W9b28CFBEmxBPz5bGd1-hw 教程pdf文件下载地址 https://files.cnblogs.com/files/ ...