Thinkphp Controller Dispatch Usage Example

Source: Internet
Author: User

  This article mainly introduces the thinkphp controller scheduling use examples, need friends can refer to the following

1. How to get the module name and controller name through the address bar parameter (even if there is a routing and rewriting module in the case)     2.TP is how to implement the predecessor, the 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-defined classes in the attribute, the method of the permissions and parameters of information, such information can be accurately control the implementation of the method   Reflectionclass Main methods:   Hasmethod (string)   existence of a method GetMethod (string)   Fetch method   Reflectionmethod Main methods:   Getnumberofparameters ()   Get the number of parameters Getparamters ()   Get parameter information   3. Code demo     code is as follows: <?php& nbsp Class indexaction{ public function Index () {   echo ' index '. " RN "; &NBSP}  public function Test ($year =2012, $month =2, $day =21) {   echo $year. '--------'. $ month. '-----------'. $day. " RN "; &NBSP}  public function _before_index () {   echo __function__. " RN "; &NBSP}  public function _after_index () {   echo __function__. " RN "; &NBSP}  //execute index method $method = new Reflectionmethod (' indexaction ', ' Index '); Permission to judge if ($method->ispublic ()) {  $class = new Reflectionclass (' indexaction ');  //Executes the predecessor Method  if ($class->hasmethod (' _before_index ')) {  $beforeMethod = $class->getmethod (' _before_index ' );   if ($beforeMethod->ispublic ()) {    $beforeMethod->invoke (new indexaction);  }  }     $method->invoke (new indexaction);    //Execution  if ($class->hasmethod (' _after_index ')) {  $beforeMethod = $class->getmethod (' _ After_index ');   if ($beforeMethod->ispublic ()) {    $beforeMethod->invoke (new indexaction);  }  } & nbsp  //execute 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 (); The If (count ($args) = = $method->getnumberofparameters ())   $method->invokeargs (new indexaction, $args); else  echo ' parameters is not match! ';    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.