国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > ThinkPHP > 正文

thinkphp控制器調度使用示例

2024-09-09 15:20:33
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了thinkphp控制器調度使用示例,需要的朋友可以參考下

1.如何通過地址欄參數來得到模塊名稱和控制器名稱(即使在有路由和開了重寫模塊的情況下)

2.tp是如何實現前置,后置方法功能模塊,和如何執行帶參數的方法?

php系統自帶的 ReflectionClass,ReflectionMethod 類,可以反射用戶自定義類的中屬性,方法的權限和參數等信息,通過這些信息可以準確的控制方法的執行

ReflectionClass主要用的方法: 

hasMethod(string)  是否存在某個方法

getMethod(string)   獲取方法

ReflectionMethod 主要方法: 

getNumberOfParameters()  獲取參數個數

getParamters()  獲取參數信息

3.代碼演示:

  1. <?php  
  2. class IndexAction{ 
  3.  public function index(){ 
  4.    echo 'index'."/r/n"
  5.  } 
  6.  public function test($year=2012,$month=2,$day=21){ 
  7.    echo $year.'--------'.$month.'-----------'.$day."/r/n"
  8.  } 
  9.  public function _before_index(){ 
  10.    echo __FUNCTION__."/r/n"
  11.  } 
  12.  public function _after_index(){ 
  13.    echo __FUNCTION__."/r/n"
  14.  } 
  15.  
  16. //執行index方法 
  17. $method = new ReflectionMethod('IndexAction','index'); 
  18. //進行權限判斷 
  19. if($method->isPublic()){ 
  20.  $class = new ReflectionClass('IndexAction'); 
  21.  //執行前置方法 
  22.  if($class->hasMethod('_before_index')){ 
  23.   $beforeMethod = $class->getMethod('_before_index'); 
  24.   if($beforeMethod->isPublic()){ 
  25.    $beforeMethod->invoke(new IndexAction); 
  26.   } 
  27.  } 
  28.  
  29.  $method->invoke(new IndexAction); 
  30.  
  31.  //執行后置方法 
  32.  if($class->hasMethod('_after_index')){ 
  33.   $beforeMethod = $class->getMethod('_after_index'); 
  34.   if($beforeMethod->isPublic()){ 
  35.    $beforeMethod->invoke(new IndexAction); 
  36.   } 
  37.  } 
  38.  
  39.  
  40. //執行帶參數的方法 
  41. $method = new ReflectionMethod('IndexAction','test'); 
  42. $params = $method->getParameters(); 
  43. foreach($params as $param ){ 
  44.  $paramName = $param->getName(); 
  45.  if(isset($_REQUEST[$paramName])) 
  46.   $args[] = $_REQUEST[$paramName]; 
  47.  elseif($param->isDefaultValueAvailable()) 
  48.   $args[] = $param->getDefaultValue(); 
  49. if(count($args)==$method->getNumberOfParameters()) 
  50.  $method->invokeArgs(new IndexAction,$args); 
  51. else 
  52.  echo 'parameters is not match!'

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 唐河县| 区。| 巴彦淖尔市| 海晏县| 云南省| 绵竹市| 深州市| 深水埗区| 阜南县| 虞城县| 安义县| 新源县| 婺源县| 罗平县| 苗栗市| 蒙城县| 通许县| 独山县| 新宁县| 耿马| 阳东县| 忻城县| 重庆市| 钦州市| 禹州市| 甘南县| 莫力| 武安市| 鹤庆县| 忻州市| 新乡县| 曲松县| 顺昌县| 峡江县| 崇州市| 合江县| 阿克| 赞皇县| 台安县| 石阡县| 河东区|