这篇文章主要介绍了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教程

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
