search
HomePHP FrameworkThinkPHPWhat is the mode of thinkphp?
What is the mode of thinkphp?Feb 14, 2022 pm 05:26 PM
mvcthinkphp

thinkphp is mvc mode. ThinkPHP is based on the MVC model and supports multi-layer (multi-Layer) design. It is an open source lightweight PHP framework born to simplify enterprise-level application development and agile WEB application development.

What is the mode of thinkphp?

The operating environment of this tutorial: Windows 7 system, thinkphp v5.1 version, Dell G3 computer.

ThinkPHP is based on the MVC model and supports multi-layer (multi-Layer) design.

ThinkPHP is a fast, compatible and simple lightweight domestic PHP development framework. It was born in early 2006, formerly known as FCS. It was officially renamed ThinkPHP on New Year's Day in 2007. It is released under the Apache2 open source agreement and is derived from Struts The structure was transplanted and improved and perfected. At the same time, it also learned from many excellent foreign frameworks and patterns, using object-oriented development structure and MVC pattern, integrating the ideas of Struts and TagLib (tag library), RoR's ORM mapping and ActiveRecord. model.

M (model) – Model class

Model

in ThinkPHP The basic model class is the Think\Model class, which completes basic CURD, ActiveRecord mode, coherent operations and statistical queries. Some advanced features are encapsulated into other model extensions.

Note: The design of the basic model class is very flexible. You can even perform ORM and CURD operations on related data tables without any model definition. Only when you need to encapsulate separate business logic , the model class must be defined.

Model definition

The model class does not have to be defined. It only needs to be defined when there is independent business logic or attributes.

Model classes usually need to inherit the system\Think\Model class or its subclasses. The following is the definition of a Home\Model\UserModel class:

    namespace Home\Model;
    use Think\Model;
    class UserModel extends Model{
    }

Model class In most cases, it is used to operate the database. If the model class is named according to the system's specifications, it can automatically correspond to the data table in most cases.

##UserModelthink_userUserTypeModelthink_user_type

V(view)– 视图层

模板定义

每个模块的模块文件是独立的,为了对模块文件更加有效的管理,ThinkPHP对模块文件进行目录划分,默认的模板文件定义规则是:

视图目录/[模块主题/]'控制器名/操作名/操作名+模板后缀

默认的视图目录是模块的View目录(模块可以有多个视图文件目录),框架的默认视图文件后缀是.html

在每个模块主题下面,是以模块下面的控制器名为目录,然后是每个控制器的具体操作模板文件,如:
User控制器的add操作 对应的模块文件就应该是:
./Application/Home/View/User/add.html
如果默认视图层不是View,设置如:
'DEFAULT_V_LAYER'=>'Template',//设置默认的视图层名称,对应的模板文件就变成了:./Application/Home/Template/User/add.html
模板文件的默认后缀是.html,可以通过TMPL_TEMPLATE_SUFFIX来配置。
'TMPL_TEMPLATE_SUFFIX'=>'.tpl'
定义后,User控制器的add操作 对应的模板文件就变成是:./Application/Home/View/User/add.tpl

模板主题

模板主题可以对相同的控制器输出进行不同的布局和样式调整
一个模块需要支持多套模板文件的话,就可以使用模板主题功能。默认情况下,没有开启模板主题功能,如果需要开启,设置DEFAULT_THEME参数即可:

// 设置默认的模板主题 <br> 'DEFAULT_THEME'=>'default' <br>
采用模板主题后,需要在视图目录下面创建对应的主题目录,和不启用模板主题的情况相比,模板文件只是多了一层目录:

View/User/add.html //没有启用模板主题之前
View/default/User/add.html //启用模板主题之后

在视图渲染输出之前,我们可以通过动态设置来改变需要使用的模板主题。

//在控制器中动态改变模板主题
$this->theme(&#39;blue&#39;)->display(&#39;add&#39;);

模板赋值

如果要在模板中输出变量,必须在控制器中把变量传递给模板,通过assign方法对模板变量赋值

$this->assign(&#39;name&#39;,$value);//下面的写法是等效的
$this->name=$value;

assign方法必须在displayshow方法之前调用,并且系统只会输出设定的变量,其他变量不会输出(系统变量例外)。

系统变量可以通过特殊的标签输出,无需赋值模板变量

赋值后,就可以在模板文件中输出变量,如果使用的是内置模板的话,就可以这样输出:{$name}
输出多个模板变量,可以使用下面的方式:

$array[&#39;name&#39;] = &#39;thinkphp&#39;;
$array[&#39;email&#39;] = &#39;fdsf@123.com&#39;;
$array[&#39;phone&#39;] = &#39;123456789&#39;;
$this->assign($array);

模板渲染

模板定义后就可以渲染模板输出,系统也支持直接渲染内容输出,模板赋值必须在模板渲染之前操作。

渲染模板

渲染模板输出最常用的是使用display方法,调用格式:
display('[模板文件]'[,'字符编码'][,'输出类型'])模板文件的写法支持下面几种:

Model name Agree on the corresponding data table (assuming the prefix definition of the data table is think_)
用法 描述
不带任何参数 自动定位当前操作的模板文件
[模块@][控制器:][操作] 常用写法,支持跨模块 模板主题可以和theme方法配合
完整的模板文件名 直接使用完整的模板文件名(包括模板后缀)

eg.

//不带任何参数 自动定位当前操作的模板文件
$this->display();

通常默认的视图目录是View

如果没有按照模板定义的规则来定义模板文件(或者需要调用其他控制器下面的某个模板),使用:

//表示调用当前控制器下面的edit模块
$this->display(&#39;edit&#39;);
//表示调用Member控制器下面的read模块
$this->display(&#39;Member:read&#39;);

如果我们使用了模板主题功能,那么也可以支持主题调用,使用:

\\表示调用blue主题下面的User控制器的edit模块
$this->theme(&#39;blue&#39;)->display(&#39;User:edit&#39;);

获取模板地址

T函数用于生成模板文件名,用法:
T([资源://][模块@][主题/][控制器/]操作,[视图分层])
T函数的返回值为一个完整的模板文件名,可以直接用于display和fetch方法进行渲染输出。
eg.

T(&#39;Public/menu&#39;);
//返回 当前模块/View/Public/menu.html
T(&#39;blue/Public/menu&#39;);
//返回 当前模块/View/blue/Public/menu.html
T(&#39;Public/menu&#39;,&#39;Tpl&#39;);
//返回 当前模块/Tpl/Public/menu.html
T(&#39;Admin@Public/menu&#39;);
//返回 Admin/View/Public/menu.html

在display方法中直接使用T函数

//使用T函数输出模板
$this->display(T(&#39;Admin@Public/menu&#39;));

T函数可以输出不同的视图分层模块。

获取内容

如果需要获取渲染模板的输出内容而不是直接输出,可以使用fetch方法。
eg.
$content = $this->fetch('Member:edit');
使用fetch方法获取渲染内容后,可以进行过滤和替换等操作。

 渲染内容

如果没有定义任何模板文件,或者把模板内容存储到数据库的话,就需要使用show方法来渲染输出。

show方法调用格式:
show(‘渲染内容’[,’字符编码’][,’输出类型’])
eg.$this->show($content);

【相关教程推荐:thinkphp框架

The above is the detailed content of What is the mode of thinkphp?. For more information, please follow other related articles on the PHP Chinese website!

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
thinkphp是不是国产框架thinkphp是不是国产框架Sep 26, 2022 pm 05:11 PM

thinkphp是国产框架。ThinkPHP是一个快速、兼容而且简单的轻量级国产PHP开发框架,是为了简化企业级应用开发和敏捷WEB应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。

一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列一起聊聊thinkphp6使用think-queue实现普通队列和延迟队列Apr 20, 2022 pm 01:07 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了关于使用think-queue来实现普通队列和延迟队列的相关内容,think-queue是thinkphp官方提供的一个消息队列服务,下面一起来看一下,希望对大家有帮助。

thinkphp的mvc分别指什么thinkphp的mvc分别指什么Jun 21, 2022 am 11:11 AM

thinkphp基于的mvc分别是指:1、m是model的缩写,表示模型,用于数据处理;2、v是view的缩写,表示视图,由View类和模板文件组成;3、c是controller的缩写,表示控制器,用于逻辑处理。mvc设计模式是一种编程思想,是一种将应用程序的逻辑层和表现层进行分离的方法。

实例详解thinkphp6使用jwt认证实例详解thinkphp6使用jwt认证Jun 24, 2022 pm 12:57 PM

本篇文章给大家带来了关于thinkphp的相关知识,其中主要介绍了使用jwt认证的问题,下面一起来看一下,希望对大家有帮助。

thinkphp扩展插件有哪些thinkphp扩展插件有哪些Jun 13, 2022 pm 05:45 PM

thinkphp扩展有:1、think-migration,是一种数据库迁移工具;2、think-orm,是一种ORM类库扩展;3、think-oracle,是一种Oracle驱动扩展;4、think-mongo,一种MongoDb扩展;5、think-soar,一种SQL语句优化扩展;6、porter,一种数据库管理工具;7、tp-jwt-auth,一个jwt身份验证扩展包。

一文教你ThinkPHP使用think-queue实现redis消息队列一文教你ThinkPHP使用think-queue实现redis消息队列Jun 28, 2022 pm 03:33 PM

本篇文章给大家带来了关于ThinkPHP的相关知识,其中主要整理了使用think-queue实现redis消息队列的相关问题,下面一起来看一下,希望对大家有帮助。

thinkphp 怎么查询库是否存在thinkphp 怎么查询库是否存在Dec 05, 2022 am 09:40 AM

thinkphp查询库是否存在的方法:1、打开相应的tp文件;2、通过“ $isTable=db()->query('SHOW TABLES LIKE '."'".$data['table_name']."'");if($isTable){...}else{...}”方式验证表是否存在即可。

thinkphp3.2怎么关闭调试模式thinkphp3.2怎么关闭调试模式Apr 25, 2022 am 10:13 AM

在thinkphp3.2中,可以利用define关闭调试模式,该标签用于变量和常量的定义,将入口文件中定义调试模式设为FALSE即可,语法为“define('APP_DEBUG', false);”;开启调试模式将参数值设置为true即可。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools