thinkphp控制器調(diào)度使用示例
1.如何通過(guò)地址欄參數(shù)來(lái)得到模塊名稱和控制器名稱(即使在有路由和開了重寫模塊的情況下)
2.tp是如何實(shí)現(xiàn)前置,后置方法功能模塊,和如何執(zhí)行帶參數(shù)的方法?
php系統(tǒng)自帶的 ReflectionClass,ReflectionMethod 類,可以反射用戶自定義類的中屬性,方法的權(quán)限和參數(shù)等信息,通過(guò)這些信息可以準(zhǔn)確的控制方法的執(zhí)行
ReflectionClass主要用的方法:
hasMethod(string) 是否存在某個(gè)方法
getMethod(string) 獲取方法
ReflectionMethod 主要方法:
getNumberOfParameters() 獲取參數(shù)個(gè)數(shù)
getParamters() 獲取參數(shù)信息
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";
}
}
//執(zhí)行index方法
$method = new ReflectionMethod('IndexAction','index');
//進(jìn)行權(quán)限判斷
if($method->isPublic()){
$class = new ReflectionClass('IndexAction');
//執(zhí)行前置方法
if($class->hasMethod('_before_index')){
$beforeMethod = $class->getMethod('_before_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
$method->invoke(new IndexAction);
//執(zhí)行后置方法
if($class->hasMethod('_after_index')){
$beforeMethod = $class->getMethod('_after_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
}
//執(zhí)行帶參數(shù)的方法
$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控制器間實(shí)現(xiàn)相互調(diào)用的方法
- ThinkPHP中URL路徑訪問(wèn)與模塊控制器之間的關(guān)系
- thinkphp3.2實(shí)現(xiàn)跨控制器調(diào)用其他模塊的方法
- thinkphp3.2實(shí)現(xiàn)上傳圖片的控制器方法
- ThinkPHP3.2.2的插件控制器功能
- Thinkphp 空操作、空控制器、命名空間(詳解)
- thinkPHP通用控制器實(shí)現(xiàn)方法示例
- thinkPHP控制器變量在模板中的顯示方法示例
- ThinkPHP5.0框架控制器繼承基類和自定義類示例
- TP(thinkPHP)框架多層控制器和多級(jí)控制器的使用示例
相關(guān)文章
彈出模態(tài)框modal的實(shí)現(xiàn)方法及實(shí)例
這篇文章主要介紹了彈出模態(tài)框modal的實(shí)現(xiàn)方法及實(shí)例的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09thinkphp5.1 中使用自定義異常處理類進(jìn)行接管
這篇文章主要介紹了thinkphp5.1 中使用自定義異常處理類進(jìn)行接管,本文通過(guò)配置文件的修改和具體代碼實(shí)現(xiàn)詳細(xì)展開的講解了如何使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07微信支付開發(fā)動(dòng)態(tài)鏈接Native支付
本篇文章主要介紹微信支付下的基于動(dòng)態(tài)鏈接二維碼的Native支付實(shí)現(xiàn)流程,希望能給開發(fā)微信支付的小伙伴提供幫助2016-07-07php bcdiv和bcmul 函數(shù)的怪異現(xiàn)象
這篇文章主要介紹了php bcdiv和bcmul 函數(shù)的怪異現(xiàn)象,本文通過(guò)實(shí)例代碼講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04php設(shè)計(jì)模式之觀察者模式的應(yīng)用詳解
本篇文章是對(duì)php中的觀察者模式進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05