您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

实例示范thinkphp控制器如何调度

2025/3/17 21:09:50发布79次查看
这篇文章主要介绍了thinkphp控制器调度使用示例,需要的朋友可以参考下
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!';
【相关教程推荐】
1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product