Introduction
This chapter is a short, casual introduction to MVC concepts as they are implemented in Cake. If you're new to MVC (Model View Controller) patterns, this chapter is definitely for you. We begin with a discussion of general MVC concepts, work our way into the specific application of MVC in CakePHP, and show some simple examples of CakePHP using the MVC pattern.
本节很短,而且是一个很随便的关于MVC和cake的话题。如果你之前对MVC这个概念不足够了解,本节会给你一个大致的定义。我开始讨论一般的MVC以及在cakePHP中MVC视如何定义的,其中会有些CakePHP的例子是使用 MVC模式的。
Section 2
The MVC Pattern
Model-View-Controller is a software design pattern that helps you logically separate your code, make it more reusable, maintainable, and generally better. Model View Controller was first described by the author group Gang of Four. Dean Helman wrote (an extract from Objective Toolkit Pro white paper):
MVC是一种设计模式帮助你在逻辑上分离代码,使得他具有更多重用性,可维护性。MVC最早是四人小组中的Helman提出的。
"The MVC paradigm is a way of breaking an application, or even just a piece of an application's interface, into three parts: the model, the view, and the controller. MVC was originally developed to map the traditional input, processing, output roles into the GUI realm.
MVC凡是是一种方法,他把应用程序分成三个部分 MODEL VIEW 和 CONTROLLER。MVC通常被用来开发映射成为 传统意义上的输入 处理 输出到界面 三个部分。
Input -> Processing -> Output
Controller -> Model -> View
"The user input, the modeling of the external world, and the visual feedback to the user are separated and handled by model, view port and controller objects. The controller interprets mouse and keyboard inputs from the user and maps these user actions into commands that are sent to the model and/or view port to effect the appropriate change. The model manages one or more data elements, responds to queries about its state, and responds to instructions to change state. The view port manages a rectangular area of the display and is responsible for presenting data to the user through a combination of graphics and text."
用户从外部世界输入一个模型(MODEL),然后可视化界面会返回给用户,通过被分离和控制的 model view 端口 以及 控制器对象。控制器从鼠标和键盘得到用户的输入并变成动作指令送到model或者view端口并给予适当的改变。model管理着一个或者多个数据源色返回这些状态的数据,然后返回被改变状态的指令。 view端口管理一个显示区域并将数据通过图形和文本形式显示给用户。
In Cake terms, the Model represents a particular database table/record, and it's relationships to other tables and records. Models also contain data validation rules, which are applied when model data is inserted or updated. The View represents Cake's view files, which are regular HTML files embedded with PHP code. Cake's Controller handles requests from the server. It takes user input (URL and POST data), applies business logic, uses Models to read and write data to and from databases and other sources, and lastly, sends output data to the appropriate view file.
To make it as easy as possible to organize your application, Cake uses this pattern not only to manage how objects interact within your application, but also how files are stored, which is detailed next.
在CakePHP中,model通常提供一个数据库的表或者记录以及他关联的其他表和记录。model还包括当model数据被插入或者更新时的数据验证规则。CakePHP的VIEW是一些嵌入了PHP代码的HTML文件。cake的控制器掌握着来自服务器的请求。他包含着用户输入(url和post 数据),适用的商务逻辑,控制model从数据库或者其他数据源读取和写入数据,最后,把输出数据送到显示文件。为了使得你的应用程序尽可能简单地组织起来,cake不只是用这些模式管理对象和你的应用的交互,也帮助管理文件怎么存储这些具体细节。
Section 3
Overview of the Cake File Layout
When you unpack Cake on your server you will find three main folders -
app cake vendors
当你解压缩cake后就会发现三个目录
The cake folder is where the core libraries for Cake lay and you generally won't ever need to touch it.
The app folder is where your application specific folders and files will go. The separation between the cake folder and the app folder make it possible for you to have many app folders sharing a single set of Cake libraries. This also makes it easy to update CakePHP: you just download the latest version of Cake and overwrite your current core libraries. No need to worry about overwriting something you wrote for your app.
cake目录防治这cake的核心代码,一般来说你不要去动它。
app目录是你的因程序的目录。他和cake目录是分开的。app目录也是的你的许多应用都可以共享一套cake lib,这样你可以很容易去升级cakePHP。你只要下载最新版本的ake然后覆盖掉你当前的核心库,不需要担心覆盖掉任何你写的应用程序。
/app /config - Contains config files for your database, ACL, etc. /controllers - Controllers go here /components - Components go here /index.php - Allows you to deploy cake with /app as the DocumentRoot /models - Models go here /plugins - Plugins go here /tmp - Used for caches and logs /vendors - Contains third-party libaries for this application /views - Views go here /elements - Elements, little bits of views, go here /errors - Your custom error pages go here /helpers - Helpers go here /layouts - Application layout files go here /pages - Static views go here /webroot - The DocumentRoot for the application /css /files /img /js /cake - Cake's core libraries. Don't edit any files here. index.php /vendors - Used for server-wide third-party libraries. VERSION.txt - Let's you know what version of Cake you're using.
You can use the vendors directory to keep third-party libraries in. You will learn more about vendors later, but the basic idea is that you can access classes you've placed in the vendors directory using Cake's vendor() function.
Let's look at the entire file layout:



2008/10/01 12:25 | by 
