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!';

标签:thinkphp,控制器
0
投稿

猜你喜欢

  • Python计算信息熵实例

    2022-08-17 16:13:39
  • Go缓冲channel和非缓冲channel的区别说明

    2024-05-22 10:11:01
  • python 3.7.0 安装配置方法图文教程

    2023-10-14 22:24:28
  • python3.7简单的爬虫实例详解

    2023-06-30 15:55:13
  • Python3读写ini配置文件的示例

    2023-05-29 22:59:21
  • python实现简单多人聊天室

    2022-08-17 15:04:13
  • JavaScript 设计模式 富有表现力的Javascript(一)

    2023-08-25 07:42:15
  • SQL Server中使用DTS设计器进行数据转移

    2009-01-08 16:15:00
  • Tornado实现多进程/多线程的HTTP服务详解

    2023-11-19 23:40:20
  • SQL Server比较常见数据类型详解

    2024-01-26 11:28:48
  • Python使用matplotlib绘制三维图形示例

    2022-09-14 10:53:14
  • DataFrame.to_excel多次写入不同Sheet的实例

    2022-03-26 01:20:14
  • python实现多线程的两种方式

    2022-09-30 00:54:15
  • oracle数据库迁移到MySQL的方法总结

    2024-01-14 08:32:05
  • 浅谈javascript中onbeforeunload与onunload事件

    2024-04-19 09:46:08
  • YOLOv5车牌识别实战教程(三)模型训练与评估

    2021-01-07 19:15:43
  • NumPy进行统计分析

    2023-09-21 00:16:15
  • pandas.DataFrame中提取特定类型dtype的列

    2021-06-13 06:04:25
  • JavaScript实现涂鸦笔功能

    2024-04-10 11:01:01
  • 运用PyTorch动手搭建一个共享单车预测器

    2022-10-20 06:25:04
  • asp之家 网络编程 m.aspxhome.com