thinkphp controller scheduling usage example_PHP tutorial

WBOY
Release: 2016-07-13 10:24:58
Original
961 people have browsed it

1. How to get the module name and controller name through the address bar parameters (even when there is routing and the rewrite module is turned on)

2.tp How to implement pre- and post-method function modules, and how to execute methods with parameters?

The ReflectionClass and ReflectionMethod classes that come with the PHP system can reflect information such as attributes of user-defined classes, method permissions and parameters, etc. Through this information, the execution of methods can be accurately controlled

The main methods used by ReflectionClass:
hasMethod(string) Whether there is a method
getMethod(string) Get the method

ReflectionMethod main method:
getNumberOfParameters() Get the number of parameters
getParamters() Get parameter information

3. Code demonstration

Copy code The code is as follows:

class IndexAction{
public function index() {
echo 'index'."rn";
}
public function test($year=2012,$month=2,$day=21){
echo $year.'-- ------'.$month.'-----------'.$day."rn";
}
public function _before_index(){
echo __FUNCTION__ ."rn";
}
public function _after_index(){
echo __FUNCTION__."rn";
}
}

//Execute the index method
$method = new ReflectionMethod('IndexAction','index');
//Perform permission judgment
if($method->isPublic()){
$class = new ReflectionClass('IndexAction');
//Execute the pre-method
if($class->hasMethod('_before_index')){
$beforeMethod = $class-> ;getMethod('_before_index');
if($beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}

$method->invoke(new IndexAction);

//Execute the post method
if($class->hasMethod('_after_index')){
$beforeMethod = $class->getMethod('_after_index');
if( $beforeMethod->isPublic()){
$beforeMethod->invoke(new IndexAction);
}
}
}


//Execute the method with parameters
$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!';

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825264.htmlTechArticle1. How to get the module name and controller name through the address bar parameters (even if there is routing and restarting In the case of writing modules) 2. How does tp implement pre- and post-method function modules, and such as...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!