Thinkphp controller scheduling example _ php instance

Source: Internet
Author: User
This article mainly introduces the thinkphp controller scheduling example. For more information, see section 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

The Code is as follows:


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! ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.