Home Backend Development PHP Tutorial thinkphp控制器调度使用示例_PHP

thinkphp控制器调度使用示例_PHP

Jun 01, 2016 am 11:56 AM
Scheduling

ThinkPHP

1.如何通过地址栏参数来得到模块名称和控制器名称(即使在有路由和开了重写模块的情况下)

2.tp是如何实现前置,后置方法功能模块,和如何执行带参数的方法?

php系统自带的 ReflectionClass,ReflectionMethod 类,可以反射用户自定义类的中属性,方法的权限和参数等信息,通过这些信息可以准确的控制方法的执行

ReflectionClass主要用的方法: 
hasMethod(string)  是否存在某个方法
getMethod(string)   获取方法

ReflectionMethod 主要方法: 
getNumberOfParameters()  获取参数个数
getParamters()  获取参数信息

3.代码演示

复制代码 代码如下:
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";
 }
}

//执行index方法
$method = new ReflectionMethod('IndexAction','index');
//进行权限判断
if($method->isPublic()){
 $class = new ReflectionClass('IndexAction');
 //执行前置方法
 if($class->hasMethod('_before_index')){
  $beforeMethod = $class->getMethod('_before_index');
  if($beforeMethod->isPublic()){
   $beforeMethod->invoke(new IndexAction);
  }
 }

 $method->invoke(new IndexAction);

 //执行后置方法
 if($class->hasMethod('_after_index')){
  $beforeMethod = $class->getMethod('_after_index');
  if($beforeMethod->isPublic()){
   $beforeMethod->invoke(new IndexAction);
  }
 }
}


//执行带参数的方法
$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!';

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to disable the 'Click to Show Desktop' feature in macOS How to disable the 'Click to Show Desktop' feature in macOS Nov 23, 2023 pm 02:31 PM

By default, macOSSonoma hides all active windows when you click on your desktop wallpaper. This is convenient if you tend to have a bunch of files on your desktop that you need to access. However, if you find this behavior maddening, there is a way to turn it off. Apple's latest macOS Sonoma Mac operating system has a new option called "Click the wallpaper to show the desktop." Enabled by default, this option can be particularly useful if you tend to have multiple windows open and want to access files or folders on your desktop without having to minimize or move the windows. When you enable the feature and click on the desktop wallpaper, all open windows are temporarily swept aside, allowing direct access to the desktop. Once done, you can again

Use go-zero to implement distributed task distribution and scheduling Use go-zero to implement distributed task distribution and scheduling Jun 22, 2023 am 09:06 AM

With the rapid development of Internet business and the gradually increasing business volume, the amount of data that a single server can process is far from meeting demand. In order to meet the requirements of high concurrency, high availability, and high performance, distributed architecture emerged as the times require. In a distributed architecture, task distribution and scheduling is a very critical component. The quality of task distribution and scheduling will directly affect the performance and stability of the entire system. Here, we will introduce how to use the go-zero framework to implement distributed task distribution and scheduling. 1. Distributed task distributionTask distribution

MySql's scheduled tasks and scheduling: How to use MySQL to achieve efficient scheduled tasks and scheduling MySql's scheduled tasks and scheduling: How to use MySQL to achieve efficient scheduled tasks and scheduling Jun 15, 2023 pm 07:47 PM

MySQL is currently one of the most widely used relational databases. It provides numerous functions and tools, including scheduled tasks and scheduling functions. In actual development, we often need to perform certain tasks regularly, such as backing up databases, generating reports, etc. At this time, MySQL's scheduled tasks and scheduling functions can come in handy. In this article, we will introduce MySQL's scheduled tasks and scheduling functions, and how to use them to achieve efficient scheduled tasks and scheduling. 1. MySQL’s scheduled tasks and scheduling functions MySQL

How to use the Hyperf framework for scheduled task scheduling How to use the Hyperf framework for scheduled task scheduling Oct 20, 2023 am 08:01 AM

How to use the Hyperf framework for scheduled task scheduling Hyperf is a high-performance and flexible PHP framework based on Swoole extension. It provides a rich set of features and components, including a powerful scheduled task scheduler. This article will introduce how to use the Hyperf framework for scheduled task scheduling and provide specific code examples. Install the Hyperf framework First, we need to install the Hyperf framework. You can use the Composer command to install: composerc

How to implement distributed scheduled tasks and scheduling in PHP microservices How to implement distributed scheduled tasks and scheduling in PHP microservices Sep 25, 2023 pm 05:54 PM

How to implement distributed scheduled tasks and scheduling in PHP microservices In modern microservice architecture, distributed scheduled tasks and scheduling are very important components. They can help developers easily manage, schedule and execute scheduled tasks in multiple microservices, improving system reliability and scalability. This article will introduce how to use PHP to implement distributed timing tasks and scheduling, and provide code examples for reference. Using a queue system In order to implement distributed scheduled tasks and scheduling, you first need to use a reliable queue system. Queuing systems can

Analysis of Linux process priority scheduling mechanism Analysis of Linux process priority scheduling mechanism Mar 15, 2024 am 09:36 AM

Title: Analysis of Linux Process Priority Scheduling Mechanism The Linux operating system is an open source operating system with powerful multi-tasking capabilities. In Linux systems, process scheduling is very important, which affects the performance and response speed of the system. In order to better process scheduling, the Linux system implements a process priority scheduling mechanism. 1. Process priority In the Linux system, each process has a priority, which is used to determine the scheduling order of the process in the system. The value range of priority is usually 0~

Application and practice of Redis in distributed task scheduling Application and practice of Redis in distributed task scheduling Jun 20, 2023 am 09:32 AM

Application and Practice of Redis in Distributed Task Scheduling With the expansion of business scale, task scheduling has become one of the key technologies in distributed systems. Among many task scheduling tools, Redis, as a storage middleware, can also provide powerful support. This article will introduce the application and practice of Redis in distributed task scheduling from aspects such as Redis as the infrastructure of distributed task scheduling, Redis queue, and task scheduler design. 1. Redis as the infrastructure for distributed task schedulingRed

C program for loop scheduling C program for loop scheduling Sep 25, 2023 pm 05:09 PM

Wearegivenwiththenprocesseswiththeircorrespondingbursttimeandtimequantumandthetaskistofindtheaveragewaitingtimeandaverageturnaroundtimeanddisplaytheresult.WhatisRoundRobinScheduling?RoundrobinisaCPUschedulingalgorithmthatisdesignedespeciallyfortimesh

See all articles