Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-06 20:16:46
Original
1104 people have browsed it

这篇文章主要介绍了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

复制代码 代码如下:

namespace Common\Controller;
use Think\Controller;
/**
* 前后台公用基类
* modify author : Jack
* modify time : 2014-7-12
*/
class BaseController extends Controller{
       
        public function _initialize() {//全局变量
               dump('基类');
            $this->cfg();
        }
}

在Home/Controller/ZixunController.class.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框架程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!