MVC 简介
5 分钟阅读
MVC Introduction - MVC 简介
Introduction to Beego’s MVC - Beego 的 MVC 简介
Beego uses a typical Model-View-Controller (MVC) framework. This diagram illustrates how request handling logic is processed:
Beego 使用典型的模型-视图-控制器 (MVC) 框架。此图表说明了如何处理请求处理逻辑:
The whole logic handling process is explained below:
整个逻辑处理过程如下所述:
- Data is recieved from the listening port. The listening port is set to 8080 by default. 从侦听端口接收数据。侦听端口默认设置为 8080。
- Beego begins processing the requested data after the request reachs port 8080 请求到达端口 8080 后,Beego 开始处理请求的数据
- The Context object is initialized. WebSocket requests will be set as Input after the request method has been verified as a standard method (get, post, put, delete, patch, options, head) in order to protect from hostile attack. 初始化 Context 对象。在将请求方法验证为标准方法(get、post、put、delete、patch、options、head)后,WebSocket 请求将被设置为 Input,以防止恶意攻击。
- If the
BeforeRouter
filter has been set by the user it is executed. IfresponseWriter
has output data while executing this filter the request will be finished and the supervise checking step (see item 21) will be performed next. 如果用户已设置BeforeRouter
过滤器,则执行该过滤器。如果responseWriter
在执行此过滤器时有输出数据,则请求将完成,然后执行监督检查步骤(参见项目 21)。 - Start handling of static files. If the requested url matches the prefix set by
StaticDir
, theServeFile
in thehttp
package will be used to handle the static file requests. 开始处理静态文件。如果请求的 url 与StaticDir
设置的前缀匹配,则http
包中的ServeFile
将用于处理静态文件请求。 - If the request is not for a static file, and the session module is enabled, the session module will initialize. An error will occur if session is being used in the
BeforeRouter
filter (see item 4). Use theAfterStatic
filter (see item 7) instead to avoid this error. 如果请求不是针对静态文件,并且启用了会话模块,则会话模块将初始化。如果在BeforeRouter
过滤器中使用会话,则会发生错误(请参阅第 4 项)。改用AfterStatic
过滤器(请参阅第 7 项)以避免此错误。 - The
AfterStatic
filter is executed. IfresponseWriter
already has output data while executing this filter the request will be finished and the supervise checking step (see item 21) will be performed next. 执行AfterStatic
过滤器。如果在执行此过滤器时responseWriter
已有输出数据,则请求将完成,接下来将执行监督检查步骤(请参阅第 21 项)。 - After all filters have been executed Beego will start to match any requested urls with fixed routing rules. These connections will only be made if the whole string matches. For example:
/hello
does not match the url of/hello/world
. If any matching pairs are found the appropriate logic will execute. 在执行完所有过滤器后,Beego 将开始将任何请求的 URL 与固定的路由规则进行匹配。仅当整个字符串匹配时才会建立这些连接。例如:/hello
与/hello/world
的 URL 不匹配。如果找到任何匹配对,则将执行相应的逻辑。 - Regex matching is executed based on the order that the user added. This means the order of the regex routing rules will affect Regex matching. If any matches are found the appropriate logic will execute. 根据用户添加的顺序执行正则表达式匹配。这意味着正则表达式路由规则的顺序将影响正则表达式匹配。如果找到任何匹配项,则将执行相应的逻辑。
- If the user registered
AutoRouter
,controller/method
will be used to match the Controller and method. If any matches are found the appropriate logic will execute. Otherwise the supervise checking step (see item 21) will be performed next. 如果用户注册了AutoRouter
,则将使用controller/method
来匹配控制器和方法。如果找到任何匹配项,则将执行相应的逻辑。否则,接下来将执行监督检查步骤(请参阅第 21 项)。 - If a Controller is found Beego will start this logic.
BeforeExec
will execute first. IfresponseWriter
already has output data while executing this filter the request will be finished and the supervise checking step (see item 21) will be performed next. 如果找到控制器,Beego 将启动此逻辑。BeforeExec
将首先执行。如果responseWriter
在执行此过滤器时已具有输出数据,则请求将完成,接下来将执行监督检查步骤(参见第 21 项)。 - Controller will start executing the
Init
function to initialize basic information.bee.Controller
will usually be initialized as part of this item, so modifiying this function is not recommened while inheriting the Controller. 控制器将开始执行Init
函数以初始化基本信息。bee.Controller
通常会作为此项的一部分进行初始化,因此在继承控制器时不建议修改此函数。 - If XSRF is enabled it will call
XsrfToken
ofController
. If this is a POST requestCheckXsrfCookie
will be called. 如果启用了 XSRF,它将调用Controller
的XsrfToken
。如果这是 POST 请求,则将调用CheckXsrfCookie
。 - The
Prepare
function ofController
will be executed. This function is normally used by the user to launch initialization. IfresponseWriter
already has data while executing this filter it will go directly to theFinish
function (see item 17). 将执行Controller
的Prepare
函数。此函数通常由用户用于启动初始化。如果responseWriter
在执行此过滤器时已具有数据,它将直接转到Finish
函数(参见第 17 项)。 - If there is no output the user registered function will be executed. If there is no function registered by the user the method in
http.Method
(GET/POST and so on) will be called. This will execute logic inluding reading data, assigning data, rendering templates, or outputing JSON or XML. 如果没有输出,将执行用户注册的函数。如果用户未注册任何函数,则将调用http.Method
(GET/POST 等)中的方法。这将执行包括读取数据、分配数据、呈现模板或输出 JSON 或 XML 在内的逻辑。 - If there is no output by
responseWrite
theRender
function will be called to output template. 如果responseWrite
没有输出,则将调用Render
函数以输出模板。 - Execute the
Finish
function ofController
. This function works as an override to allow the user to release resources, such as data initialized inInit
. 执行Controller
的Finish
函数。此函数用作覆盖,允许用户释放资源,例如在Init
中初始化的数据。 - Execute the
AfterExec
filter. If there is output it will jump to the supervise checking step (see item 21). 执行AfterExec
过滤器。如果有输出,它将跳转到监督检查步骤(参见项目 21)。 - Execute
Destructor
inController
to release data allocated inInit
. 在Controller
中执行Destructor
以释放Init
中分配的数据。 - If there is no router has been found the 404 page will be shown. 如果没有找到路由器,将显示 404 页面。
- Eventually, all logic paths lead to supervise checking. If the supervisor module is enabled (default on port 8088), the request will be sent to supervisor module to log data such as QPS of the request, visiting time, and request url. 最终,所有逻辑路径都会导致监督检查。如果启用了监督模块(默认端口 8088),则会将请求发送到监督模块以记录数据,例如请求的 QPS、访问时间和请求 URL。
The next sections will detail the first step of Beego’s MVC, routing:
下一节将详细介绍 Beego 的 MVC 的第一步,路由:
- Routing 路由
- Controller functions 控制器函数
- Cross-site request forgery (XSRF) 跨站点请求伪造 (XSRF)
- Session control 会话控制
- Message flashing 消息闪烁
- Accessing Request Data 访问请求数据
- Multiple Response Formats 多种响应格式
- Form validation 表单验证
- Rendering templates 渲染模板
- Template functions 模板函数
- Error handling 错误处理
- Handling static files 处理静态文件
- Parameter configuration 参数配置