1. How to get the module name and the controller name through the address bar parameter (even if there is a route and the rewrite module is open)
2.TP is how to implement the front, Post method function module, and how to carry out the method with parameters?
PHP System with the Reflectionclass,reflectionmethod class, you can reflect the user's custom classes in the attribute, the method of the permissions and parameters, such as information, through this information can accurately control the implementation of the method
The main methods used in Reflectionclass:
Hasmethod (String) there is a method
GetMethod (String) Fetch method
Reflectionmethod Main methods:
Getnumberofparameters () Get the number of parameters
Getparamters () get parameter information
3. Code Demo
Copy Code code as follows:
<?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 ";
}
}
Execute the Index method
$method = new Reflectionmethod (' indexaction ', ' Index ');
Make permission Judgments
if ($method->ispublic ()) {
$class = new Reflectionclass (' indexaction ');
To perform a predecessor method
if ($class->hasmethod (' _before_index ')) {
$beforeMethod = $class->getmethod (' _before_index ');
if ($beforeMethod->ispublic ()) {
$beforeMethod->invoke (new indexaction);
}
}
$method->invoke (new indexaction);
To perform a post-placement method
if ($class->hasmethod (' _after_index ')) {
$beforeMethod = $class->getmethod (' _after_index ');
if ($beforeMethod->ispublic ()) {
$beforeMethod->invoke (new indexaction);
}
}
}
To perform a 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! ';