本文实例讲述了thinkphp多层MVC用法。分享给大家供大家参考,具体如下:
ThinkPHP支持多层设计。
1.模型层Model
使用多层目录结构和命名规范来设计多层的model,例如在项目设计中如果需要区分数据层,逻辑层,服务层等不同的模型层可以在模块目录下创建Model,Logic,Service目录,把对用户表的所有模型操作分成3层。
1.Model/UserModel用于定义数据相关的自动验证,自动完成和数据存取接口
2.Logic/UserLogical用于定义用户相关的业务逻辑
3.Service/UserService用于定于用户相关的服务接口
这三个模型都继承Model类即可例如数据层Home/Model/UserModel.class.php
namespace Home\Model; use Think\Model; class UserModel extends Model{}
逻辑层Home/Logic/UserLogical.class.php
namespace Home\Logic; use Think\Model; class UserLogic extends Model{}
服务层Home/Service/UserService.class.php
namespace Home\Service; use Think\Model; class UserService extends Model{}
在调用的时候可以使用内置的D方法或M方法经行调用
D('User') //实例化UserModel D('User','Logic') //实例化UserLogic D('User','Service') //实例化UserService
调用默认的模型层Model下的数据存取接口类的时候没有第二个参数模型文件名称,默认的模型层是Model,也可以更改设置如下:
代码如下:
'DEFAULT_M_LAYER' => 'Logic', // 更改默认的模型层名称为Logic
这样的话实例化方法就需要相应的修改了
D('User') //实例化UserLogic D('User','Model') //实例化UserModel D('User','Service') //实例化UserService
可以看到使用D('User')默认情况下会去实例化UserLogice类了,这个是很灵活的,如果我们数据验证,自动完成是在js里面完成的,而取数据是从service接口中完成的,这样完全可以只要一个Service层,其他的层就不需要了。
2.视图层View
视图层由模板和模板引擎组成,常见的第三方模板是.tpl,可以直接在模板中使用php代码,视图的多层可以简单的使用目录(模板主题)来区分,例如:
View/default/User/add.html
View/blue/User/add.html
复杂一点的多层视图还可以使用不同的视图目录来区分例如:
view 普通视图层目录
mobile 手机端访问视图层目录
这样不同的模板可以使用不同的页面风格,还可以默认视图目录,如下:
代码如下:
'DEFAULT_V_LAYER' => 'Mobile', // 默认的视图层名称更改为Mobile
3.控制器层Controller
ThinkPHP的控制器有两种类别,一种是核心控制器,一种是业务控制器,核心控制器在ThinkPHP目录下,例如thinkphp\ThinkPHP\Library\Think\Controller\HproseController.class.php,负责应用的调度控制,包括Http请求的拦截,转发,加载配置等。我们这里要讨论的是业务控制器,由用户自己定义的控制器类完成,多层业务控制器的实现原理和模型的分层类似,例如业务控制器和事件控制器,
Controller/UserController //用于用户的业务逻辑控制和调度 Event/UserEvent //用于用户的事件响应操作
事件这个还没有用过,看上去很高上大,web开发中的用户事件很少,大多数在js中完成。
访问控制器Home/Controller/UserController.class.php定义如下:
namespace Home\Controller; use Think\Controller; class UserController extends Controller{ }
事件控制器Home/Event/UserEvent.class.php的定义如下:
namespace Home\Event; use Think\Controller; class UserEvent extends Controller{ }
UserContrlller负责外部交互响应,通过URL请求响应,例如http://serverName/User/index,UserEvent负责内部事件响应并且只能在内部调用A('User','Event');同样我们可以设置默认的控制器层:
代码如下:
'DEFAULT_C_LAYER' => 'Event', // 默认的控制器层名称改为Event
内部和外部是隔离的,多层控制器也不是强制的,可以根据应用的需要自由的分层,控制器里可以根据需要调用不同的分层模型,也可以显示不同的分层视图,实现不同的主题。
在MVC的三层中,ThinkPHP并不依赖M和V,可以只有C或者只有V,用户只需要定义视图,在没有C的情况下也能自动识别,但是这种怪异的写法会让很多刚刚入门的程序员非常的迷惑。
多层设计在目前的项目中还没有用到,在.net项目中倒见到很多,下次用到再做补充。
希望本文所述对大家基于thinkPHP框架的PHP程序设计有所帮助。

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1
Powerful PHP integrated development environment