分类分类
更新时间:2026-08-08 12:25:11作者:网络
这篇文章主要介绍了thinkphp控制器调度使用示例,需要的朋友可以参考下
1.如何通过地址栏参数来得到模块名称和控制器名称(即使在有路由和开了重写模块的情况下)
2.tp是如何实现前置,后置方法功能模块,和如何执行带参数的方法?
php系统自带的 ReflectionClass,ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行
ReflectionClass主要用的方法:
hasMethod(string) 是否存在某个方法
getMethod(string) 获取方法
ReflectionMethod 主要方法:
getNumberOfParameters() 获取参数个数
getParamters() 获取参数信息
3.代码演示
//执行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下载地址:http://www.a5xiazai.com/php/73011.html
相关
文字密室逃脱手机版冒险游戏255.16 MBv1.62026-08-07
下载苏丹的游戏手机版角色扮演1.02Gv1.0.62026-08-07
下载超市大亨3D手游经营养成253.14 Mv10.252026-08-07
下载你只能推搡动作射击44.62 Mv1.102026-08-07
下载仙逆本尊手游角色扮演298.41 Mv0.0.42026-08-07
下载天使帝国4手机版角色扮演837.88 Mv1.02026-08-07
下载丹墨三国弈手机版策略游戏2.14Gv1.0.02026-08-07
下载CM语音App社交通讯95.16 Mv8.16.02026-08-07
下载光氪灵动app实用工具7.6 Mv1.2.12026-08-07
下载逃出沙盒世界游戏角色扮演247.76 Mv1.0.12026-08-07
下载花选app社交通讯86.6 Mv1.4.4.02026-08-07
下载全域突击动作射击533.41 Mv1.2.32026-08-07
下载










