PHP头条
热点:

thinkphp3.2.2前后台公用类架构问题,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中

<?php
namespace 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就可以了。

www.phpzy.comtrue/php/29925.htmlTechArticlethinkphp3.2.2前后台公用类架构问题,thinkphp3.2.2公用 3.13之前好多项目都使用前后台公用类,在lib/action下创建Baseaction做为公共继承类,发现3.2.2里面很多人都用A调用,这样每用一次要用...

相关文章

PHP之友评论

今天推荐