thinkphp控制器调度使用示例
时间:2023-11-16 12:34:15
1.如何通过地址栏参数来得到模块名称和控制器名称(即使在有路由和开了重写模块的情况下)
2.tp是如何实现前置,后置方 * 能模块,和如何执行带参数的方法?
php系统自带的 ReflectionClass,ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行
ReflectionClass主要用的方法:
hasMethod(string) 是否存在某个方法
getMethod(string) 获取方法
ReflectionMethod 主要方法:
getNumberOfParameters() 获取参数个数
getParamters() 获取参数信息
3.代码演示
<?php
class IndexAction{
public function index(){
echo 'index'."\r\n";
}
public function test($year=2012,$month=2,$day=21){
echo $year.'--------'.$month.'-----------'.$day."\r\n";
}
public function _before_index(){
echo __FUNCTION__."\r\n";
}
public function _after_index(){
echo __FUNCTION__."\r\n";
}
}
//执行index方法
$method = new ReflectionMethod('IndexAction','index');
//进行权限判断
if($method->isPublic()){
$class = new ReflectionClass('IndexAction');
//执行前置方法
if($class->hasMethod('_before_index')){
$beforeMethod = $class->getMethod('_before_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
$method->invoke(new IndexAction);
//执行后置方法
if($class->hasMethod('_after_index')){
$beforeMethod = $class->getMethod('_after_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
}
//执行带参数的方法
$method = new ReflectionMethod('IndexAction','test');
$params = $method->getParameters();
foreach($params as $param ){
$paramName = $param->getName();
if(isset($_REQUEST[$paramName]))
$args[] = $_REQUEST[$paramName];
elseif($param->isDefaultValueAvailable())
$args[] = $param->getDefaultValue();
}
if(count($args)==$method->getNumberOfParameters())
$method->invokeArgs(new IndexAction,$args);
else
echo 'parameters is not match!';
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python计算信息熵实例
![](https://img.aspxhome.com/file/2023/0/103300_0s.jpg)
Go缓冲channel和非缓冲channel的区别说明
python 3.7.0 安装配置方法图文教程
![](https://img.aspxhome.com/file/2023/2/65522_0s.jpg)
python3.7简单的爬虫实例详解
Python3读写ini配置文件的示例
python实现简单多人聊天室
![](https://img.aspxhome.com/file/2023/7/67887_0s.jpg)
JavaScript 设计模式 富有表现力的Javascript(一)
SQL Server中使用DTS设计器进行数据转移
Tornado实现多进程/多线程的HTTP服务详解
SQL Server比较常见数据类型详解
Python使用matplotlib绘制三维图形示例
![](https://img.aspxhome.com/file/2023/4/121644_0s.png)
DataFrame.to_excel多次写入不同Sheet的实例
python实现多线程的两种方式
oracle数据库迁移到MySQL的方法总结
![](https://img.aspxhome.com/file/2023/4/64854_0s.png)
浅谈javascript中onbeforeunload与onunload事件
YOLOv5车牌识别实战教程(三)模型训练与评估
![](https://img.aspxhome.com/file/2023/3/106993_0s.png)
NumPy进行统计分析
![](https://img.aspxhome.com/file/2023/7/97007_0s.png)
pandas.DataFrame中提取特定类型dtype的列
JavaScript实现涂鸦笔功能
![](https://img.aspxhome.com/file/2023/5/136815_0s.jpg)
运用PyTorch动手搭建一个共享单车预测器
![](https://img.aspxhome.com/file/2023/0/95300_0s.png)