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 ...
随机推荐
- Python自动化之常用模块学习
自动化常用模块 urllib和request模块学习笔记 '获取页面,UI自动化校验页面展示作用': #-*- coding : utf-8 -*-import urllib.requestimpor ...
- 浅析websocket的基本应用spring boot + vue +C# + WPF
1.基本概念 首先websocket是基于H5的一种通信.在网页中如果定时获取服务器端的实时数据,我们常采用long poll 和ajax轮询的方式.但是在轮询过程中,由于根本没有新数据的改变,而造成 ...
- 免费内网穿透服务Localtunnel
Localtunnel 将为您分配一个唯一的可公开访问的 url,它将所有请求代理到您本地运行的网络服务器. 快速开始 全局安装 Localtunnel(需要 NodeJS)以使其在任何地方都可以访问 ...
- openstack中Nova组件简解
一.Nova组件概述 计算节点通过Nova Computer进行虚拟机创建,通过libvirt调用kvm创建虚拟机,nova之间通信通过rabbitMQ队列进行通信. Nova位于Openstack架 ...
- Android同屏、摄像头RTMP推送常用的数据接口设计探讨
前言 好多开发者在调用Android平台RTMP推送或轻量级RTSP服务接口时,采集到的video数据类型多样化,如420sp.I420.yv12.nv21.rgb的,还有的拿到的图像是倒置的,如果开 ...
- ProxySQL(11):链式规则( flagIN 和 flagOUT )
文章转载自:https://www.cnblogs.com/f-ck-need-u/p/9350631.html 理解链式规则 在mysql_query_rules表中,有两个特殊字段"fl ...
- Elasitcsearch7.X集群/索引备份与恢复实战
文章转载自:https://mp.weixin.qq.com/s/_0RlojDsE30CeDSyLNP44w 1.问题引出 ES中文社区中,有如下问题: 问题1:存储数据,data目录从一个机器直接 ...
- 使用Elasticsearch的processors来对csv格式数据进行解析
来源数据是一个csv文件,具体内容如下图所示: 导入数据到es中 有两种办法,第一种是在kibana界面直接上传文件导入 第二种方法是使用filebeat读取文件导入 这里采用第二种办法 配置文件名: ...
- [ML从入门到入门] 支持向量机:从SVM的推导过程到SMO的收敛性讨论
前言 支持向量机(Support Vector Machine,SVM)在70年代由苏联人 Vladimir Vapnik 提出,主要用于处理二分类问题,也就是研究如何区分两类事物. 本文主要介绍支持 ...
- 微信小程序发布与支付
一.小程序的发布流程 小程序协同工作和发布官网链接 1.背景 小程序的平台里,开发者完成开发之后,需要在开发者工具提交小程序的代码包,然后在小程序后台发布小程序. 2.流程 上传代码 代码管理服务器上 ...