1. How to obtain the module name and controller name through the address bar parameters (even if there is a route and a rewrite module is enabled)
2. How does tp implement the pre-and post-method functional modules and how does it execute methods with parameters?
The ReflectionClass and reflemethod method class provided by the php system can reflect attributes of user-defined classes, permissions of methods, parameters, and other information. Through this information, you can accurately control the execution of methods.
ReflectionClass mainly uses the following methods:
HasMethod (string) whether a method exists
GetMethod (string)
ReflectionMethod:
GetNumberOfParameters ()
GetParamters () Get parameter information
3. Code demonstration
Copy codeThe Code is 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 ');
// Determine Permissions
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! ';