首页 > Web开发 > 详细

thinkphp3.2实现跨控制器调用其他模块的方法

时间:2020-06-23 12:37:31      阅读:55      评论:0      收藏:0      [点我收藏+]

本文实例讲述了thinkphp3.2实现跨控制器调用其他模块的方法。分享给大家供大家参考,具体如下:

thinphp中前台后台都有互相调用方法,这样可以省去重复内容。

1
2
$hello = new \Admin\Common\Fun\hello();
$hello->hehe();

调用其他地方的方法同理。

如果是在同控制器里模块名可以省略。

如调用common里面的某个类的方法:

1
2
$hello = new \Common\Fun\hello();
$hello->hehe();

框架里面提供了跨模块、夸控制器的 A() 方法

1
2
3
4
5
6
7
class GoodsController extends Controller{
  function showlist(){
    // 实例化User控制器与调用方法
    $user = A(‘User‘);//通过快捷函数实例化控制器对象
    echo $user->number();//调用number()方法
  }
}

调用示范:

1
2
3
A(‘User‘);  //跨控制器
A(‘Admin/User‘);  //跨模块
A(shop://Admin/User);  //跨项目

如果还是不够方便的话框架还提供了R()方法,实例化类并调用方法。

1
2
3
4
//User为控制器 number为方法
R(‘User/number‘);
R(‘Admin/User/number‘);
 

效果如下:

1
2
3
4
5
6
class GoodsController extends Controller{
  function showlist(){
    // 实例化User控制器与调用方法
        A(‘User/number‘);//实例化user类并调用number方法
  }
}

thinkphp3.2实现跨控制器调用其他模块的方法

原文:https://www.cnblogs.com/microtiger/p/13181127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!