您当前的位置: 首页 > 网站编程 > PHP教程 > thinkphp控制器调度使用示例

thinkphp控制器调度使用示例

作者:不详 来源:网络 发布时间: 2014-08-09 22:43 点击:
这篇文章主要介绍了thinkphp控制器调度使用示例,需要的朋友可以参考下 1.如何通过地址栏参数来得到模块名称和控制器名称(即使在有路由和开了重写模块的情况下) 2.tp是如何实现前置,后置方法功能模块,和如何执行带参数的方法? php系统自带的 ReflectionClass,Reflect

thinkphp控制器调度使用示例

  这篇文章主要介绍了thinkphp控制器调度使用示例,需要的朋友可以参考下

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

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

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

  ReflectionClass主要用的方法:

  hasMethod(string) 是否存在某个方法

  getMethod(string) 获取方法

  ReflectionMethod 主要方法:

  getNumberOfParameters() 获取参数个数

  getParamters() 获取参数信息

  3.代码演示

  代码如下:

  <?php

  class IndexAction{

  public function index(){

  echo 'index'."rn";

  }

  public function test($year=2012,$month=2,$day=21){

  echo $year.'--------'.$month.'-----------'.$day."rn";

  }

  public function _before_index(){

  echo __FUNCTION__."rn";

  }

  public function _after_index(){

  echo __FUNCTION__."rn";

  }

  }

  //执行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!';
分享到:
本文"thinkphp控制器调度使用示例"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4