


Although Model-View-Controller (MVC) is known to almost every web developer, how is it used in actual application development? The reasonable use of MVC still troubles many people. The core idea behind MC is the reusability of code and the separation of logic and views. In this section, we will describe how to better use MVC to develop applications when using the Yii framework. For better explanation, we assume that the Web application contains the following sub-applications:
- Front end: the public website interface for end users;
- Backend: Provides management functions for managing the entire website application, usually only administrators can access and use;
- Console: Can be run in a terminal window Applications that include console commands;
- Web API: Provides an interface for third parties to interact with this application.
- These sub-applications may be implemented in the form of modules, or they may be Yii applications that share code with other sub-applications.
1. Model
Models represent the underlying data structure of a Web application. Models are often shared among different sub-applications of a Web application. For example, a
LoginForm model may be used by both the front end and the back end of an application; a News
model may be used by the console commands, Web APIs, and the front/back end of an application. Therefore, models
- should contain properties to represent specific data;
- should contain business logic (e.g. validation rules) to ensure the represented data fulfills the design requirement;
- may contain code for manipulating data. For example, a
- SearchForm
model, besides representing the search input data, may contain a
search
method to implement the actual search. Sometimes, following the last rule above may make a model very fat, containing too much code in a single class. It may also make the model hard to maintain if the code it contains serves different purposes. For example, a
model may contain a method named getLatestNews
which is only used by the front end; it may also contain a method named getDeletedNews
which is only used by the back end. This may be fine for an application of small to medium size. For large applications, the following strategy may be used to make models more maintainable:
- Define a
- NewsBase
model class which only contains code shared by different sub-applications (e.g. front end, back end);
In each sub-application, define a - News
model by extending from
NewsBase
. Place all of the code that is specific to the sub-application in thisNews
model . So, if we were to employ this strategy in our above example, we would add a
model in the front end application that contains only the getLatestNews
method, and we would add another News
model in the back end application, which contains only the getDeletedNews
method.In general, models should not contain logic that deals directly with end users. More specifically, models
- should not use
- $_GET
,
$_POST
, or other similar variables that are directly tied to the end-user request. Remember that a model may be used by a totally different sub-application (e.g. unit test, Web API) that may not use these variables to represent user requests. These variables pertaining to the user request should be handled by the Controller. should avoid embedding HTML or other presentational code. Because presentational code varies according to end user requirements (e.g. front end and back end may show the detail of a news in completely different formats), it is better taken care of by views. - 2. Views
Views are responsible for presenting models in the format that end users desire. In general, views
- should mainly contain presentational code, such as HTML, and simple PHP code to traverse, format and render data;
- should avoid containing code that performs explicit DB queries. Such code is better placed in models.
- should avoid direct access to
- $_GET
,
$_POST
, or other similar variables that represent the end user request. This is the controller's job. The view should be focused on the display and layout of the data provided to it by the controller and/or model, but not attempting to access request variables or the database directly. may access properties and methods of controllers and models directly. However, this should be done only for the purpose of presentation.
Views can be reused in different ways:
Layout: common presentational areas (e.g. page header, footer) can be put in a layout view.
Partial views: use partial views (views that are not decorated by layouts) to reuse fragments of presentational code. For example, we use
_form.php
partial view to render the model input form that is used in both model creation and updating pages.Widgets: if a lot of logic is needed to present a partial view, the partial view can be turned into a widget whose class file is the best place to contain this logic. For widgets that generate a lot of HTML markup, it is best to use view files specific to the widget to contain the markup.
Helper classes: in views we often need some code snippets to do tiny tasks such as formatting data or generating HTML tags. Rather than placing this code directly into the view files, a better approach is to place all of these code snippets in a view helper class. Then, just use the helper class in your view files. Yii provides an example of this approach. Yii has a powerful CHtml helper class that can produce commonly used HTML code. Helper classes may be put in an autoloadable directory so that they can be used without explicit class inclusion.
3. 控制器
Controllers are the glue that binds models, views and other components together into a runnable application. Controllers are responsible for dealing directly with end user requests. Therefore, controllers
may access
$_GET
,$_POST
and other PHP variables that represent user requests;may create model instances and manage their life cycles. For example, in a typical model update action, the controller may first create the model instance; then populate the model with the user input from
$_POST
; after saving the model successfully, the controller may redirect the user browser to the model detail page. Note that the actual implementation of saving a model should be located in the model instead of the controller.should avoid containing embedded SQL statements, which are better kept in models.
should avoid containing any HTML or any other presentational markup. This is better kept in views.
In a well-designed MVC application, controllers are often very thin, containing probably only a few dozen lines of code; while models are very fat, containing most of the code responsible for representing and manipulating the data. This is because the data structure and business logic represented by models is typically very specific to the particular application, and needs to be heavily customized to meet the specific application requirements; while controller logic often follows a similar pattern across applications and therefore may well be simplified by the underlying framework or the base classes.
以上就是Yii框架官方指南系列15——基础知识:最佳MVC实践的内容,更多相关内容请关注PHP中文网(www.php.cn)!

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
