本文实例讲述了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,也可以更改设置如下:
这样的话实例化方法就需要相应的修改了
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 手机端访问视图层目录
这样不同的模板可以使用不同的页面风格,还可以默认视图目录,如下:
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');同样我们可以设置默认的控制器层:
内部和外部是隔离的,多层控制器也不是强制的,可以根据应用的需要自由的分层,控制器里可以根据需要调用不同的分层模型,也可以显示不同的分层视图,实现不同的主题。
在MVC的三层中,ThinkPHP并不依赖M和V,可以只有C或者只有V,用户只需要定义视图,在没有C的情况下也能自动识别,但是这种怪异的写法会让很多刚刚入门的程序员非常的迷惑。
多层设计在目前的项目中还没有用到,在.net项目中倒见到很多,下次用到再做补充。
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》及《ThinkPHP常用方法总结》
希望本文所述对大家基于thinkPHP框架的PHP程序设计有所帮助。

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc


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

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

Hot Article

Hot Tools

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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
