国产探花免费观看_亚洲丰满少妇自慰呻吟_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!'

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

圖片精選

主站蜘蛛池模板: 论坛| 海阳市| 滕州市| 房产| 通化县| 安新县| 锡林郭勒盟| 盈江县| 新河县| 左权县| 卢湾区| 商河县| 苏州市| 平泉县| 崇礼县| 丹巴县| 同仁县| 古蔺县| 崇左市| 黑龙江省| 太保市| 图片| 冷水江市| 龙游县| 宁夏| 开阳县| 大足县| 井陉县| 临泉县| 阳新县| 华池县| 海城市| 凯里市| 宝应县| 石景山区| 固原市| 锡林郭勒盟| 左权县| 昌图县| 五大连池市| 平邑县|