·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> thinkphp3.2.2前后台公用类架构问题

thinkphp3.2.2前后台公用类架构问题

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
thinkphp3.2.2前后台公用类架构问题

3.13之前好多项目都使用前后台公用类,在lib/action下创建Baseaction做为公共继承类,发现3.2.2里面很多人都用A调用,这样每用一次要用A调用,好麻烦,小编特意偷懒。亲测使用以下方法解决。欢迎加强thinkphp3.2.2的这么创建application/Common/Controller/BaseController.class.php

 1 <?php 2 namespace Common\Controller; 3 use Think\Controller; 4     /** 5     * 前后台公用基类 6     * modify author : Jack 7     * modify time : 2014-7-12 8     */ 9     class BaseController extends Controller{10         11         public function _initialize() {//全局变量12                dump('基类');13             $this->cfg();14         }15      }

在Home/Controller/ZixunController.class.php中

<?phpnamespace Home\Controller;use Common\Controller\BaseController;class ZixunController extends BaseController {    public function index() {        $result = $this->lists();        dump($result);    }}

当然,在前后台还可以创建自己的基类,比如后台建AdminController.class.php继承BaseController.class.php,前台创建HomeController.class.php继承BaseController.class.php各自模块继承各自的基类,这样项目可以更清晰,可以避免重复造轮子,省很多事情,但是必须注意的是每个类必须声明命名空间,但是使用的资源可以在各自的基类中定义之后后面不用在写一次。比如AdminController.class.php继承BaseController.class.php,就不用再写use Think\Controller了,直接使用use Common\Controller\BaseController就可以了。