Home  >  Article  >  PHP Framework  >  What is the mode of thinkphp?

What is the mode of thinkphp?

青灯夜游
青灯夜游Original
2022-02-14 17:26:082218browse

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
Previous article:what is orm in thinkphpNext article:what is orm in thinkphp