• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Yii框架官方指南系列15——基础知识:最佳MVC实践

PHP admin 12个月前 (06-08) 9次浏览 未收录 0个评论
文章目录[隐藏]

尽管模型——视图——控制器 (MVC) 被几乎每一个Web开发人员所熟知,但是在实际应用开发中如何合理使用MVC依然困扰着许多人。MC背后的核心思想是代码的可重用性以及逻辑与视图的分离。 在本节中, 我们将会讲述在使用Yii框架的过程中如何更好的使用MVC开发应用。

为了更好进行解释阐述,我们假设Web应用包含了如下的一些子应用:

    这些子应用可能以模块的形式实现,也可能是和其他子应用共享代码的Yii应用。

    1. 模型

    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

      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 News 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:

        So, if we were to employ this strategy in our above example, we would add a News 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

          2. 视图

          Views are responsible for presenting models in the format that end users desire. In general, views

            Views can be reused in different ways:

              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

                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)!


                39MO , 版权所有丨如未注明 , 均为原创丨转载请注明原文链接:Yii框架官方指南系列15——基础知识:最佳MVC实践
                喜欢 (0)
                [1353713598@qq.com]
                分享 (0)
                发表我的评论
                取消评论
                表情 贴图 加粗 删除线 居中 斜体 签到

                Hi,您需要填写昵称和邮箱!

                • 昵称 (必填)
                • 邮箱 (必填)
                • 网址