search
Homephp教程php手册thinkphp3.2.2前后台公用类架构问题分析

这篇文章主要介绍了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框架程序设计有所帮助。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software