Home >PHP Framework >ThinkPHP >What is the mode of thinkphp?
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.
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.
Model name | Agree on the corresponding data table (assuming the prefix definition of the data table is think_) |
---|---|
think_user | |
think_user_type |
用法 | 描述 |
---|---|
不带任何参数 | 自动定位当前操作的模板文件 |
[模块@][控制器:][操作] | 常用写法,支持跨模块 模板主题可以和theme方法配合 |
完整的模板文件名 | 直接使用完整的模板文件名(包括模板后缀) |
eg.
//不带任何参数 自动定位当前操作的模板文件 $this->display();
通常默认的视图目录是View
如果没有按照模板定义的规则来定义模板文件(或者需要调用其他控制器下面的某个模板),使用:
//表示调用当前控制器下面的edit模块 $this->display('edit'); //表示调用Member控制器下面的read模块 $this->display('Member:read');
如果我们使用了模板主题功能,那么也可以支持主题调用,使用:
\\表示调用blue主题下面的User控制器的edit模块 $this->theme('blue')->display('User:edit');
获取模板地址
T函数用于生成模板文件名,用法: T([资源://][模块@][主题/][控制器/]操作,[视图分层])
T函数的返回值为一个完整的模板文件名,可以直接用于display和fetch方法进行渲染输出。
eg.
T('Public/menu'); //返回 当前模块/View/Public/menu.html T('blue/Public/menu'); //返回 当前模块/View/blue/Public/menu.html T('Public/menu','Tpl'); //返回 当前模块/Tpl/Public/menu.html T('Admin@Public/menu'); //返回 Admin/View/Public/menu.html
在display方法中直接使用T函数
//使用T函数输出模板 $this->display(T('Admin@Public/menu'));
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!