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

代码如下:
public function _initialize() {//全局变量
dump('基类');
$this->cfg();
}
}

在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就可以了。

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

相关文章

(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` ...
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的...
(1)创建模版:/App/Home/View/Form/edit.html   <FORM m...
自定义配置文件user.php: <?php return array(    \'se...
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦...
废话不多说先上图预览下,即本博客的分页; 这个分页类是在...