{"id":1660,"date":"2020-10-04T00:19:49","date_gmt":"2020-10-03T16:19:49","guid":{"rendered":"https:\/\/www.develop-note.com\/blog\/?p=1660"},"modified":"2022-02-16T21:53:56","modified_gmt":"2022-02-16T13:53:56","slug":"iris-mvc-2021ironman","status":"publish","type":"post","link":"https:\/\/www.develop-note.com\/blog\/2020\/10\/04\/iris-mvc-2021ironman\/","title":{"rendered":"Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684mvc"},"content":{"rendered":"<h1>MVC<\/h1>\n<p>\u5728\u914d\u7f6e\u597d iris \u7684\u5c08\u6848\u4e4b\u5f8c\uff0c\u63a5\u4e0b\u4f86\u5148\u4ecb\u7d39 MVC \u7684\u57fa\u672c\u6982\u5ff5\uff0c\u518d\u4f86\u4ecb\u7d39 iris \u7684 MVC \u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u5982\u4f55\u958b\u767c\u3002<\/p>\n<p><!--more--><\/p>\n<h2>MVC \u662f\u4ec0\u9ebc<\/h2>\n<p>\u6240\u8b02\u7684 MVC \u5c31\u662f <code>model<\/code>\u3001<code>view<\/code>\u3001<code>controller<\/code>\u7684\u7c21\u7a31\uff0c\u4ed6\u662f\u4e00\u500b\u5206\u5c64\u8ca0\u8cac\u7684\u6982\u5ff5\uff0c\u5c31\u662f\u628a\u756b\u9762\u5448\u73fe\u4ea4\u7d66 <code>view<\/code> \u7ba1\u7406\uff0c\u8acb\u6c42\u7684\u8f49\u767c\u53ca\u8655\u7406\u4ea4\u4ed8\u7d66<code>controller<\/code> \u4f86\u8ca0\u8cac\uff0c\u6700\u5f8c\u4e2d\u9593\u7528\u5171\u540c\u7684 <code>model<\/code> \u4f86\u6e9d\u901a\u3002\u8a73\u60c5\u5167\u5bb9\u53ef\u53c3\u8003<a href=\"https:\/\/zh.m.wikipedia.org\/zh-tw\/MVC\" title=\"MVC Wikipedia\" rel=\"nofollow noopener\" target=\"_blank\">MVC<\/a>\u3002<\/p>\n<h2>iris \u7684 MVC<\/h2>\n<p>\u63a5\u4e0b\u4f86\u8981\u8ddf\u5927\u5bb6\u4ecb\u7d39 iris \u6846\u67b6\u4e0b\u5982\u4f55\u958b\u767c\u4e00\u500b MVC \u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u9996\u5148\u6703\u5148\u8ddf\u5927\u5bb6\u4ecb\u7d39\u5982\u4f55\u5efa\u7acb\u4e00\u500b MVC \u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5e8f\uff0c\u63a5\u4e0b\u4f86\u4ecb\u7d39 <code>controller<\/code> \u7684\u7d81\u5b9a\uff0c\u6700\u5f8c\u518d\u4ecb\u7d39\u5982\u4f55\u7d81\u5b9a <code>controller<\/code> \u4e0a\u7684\u65b9\u6cd5\uff0c\u4ee5\u53ca\u8a72\u65b9\u6cd5\u5982\u4f55\u5c0d\u61c9\u5230 url \u7684\u8acb\u6c42\uff0c\u6240\u4ee5\u9996\u5148\u5148\u8a2d\u5b9a\u4e00\u500b iris\u6846\u67b6\u7684MVC\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u3002<\/p>\n<h3>iris \u7684 MVC \u7db2\u9801\u61c9\u7528\u7a0b\u5f0f<\/h3>\n<p>\u5728\u524d\u4e00\u7bc7\u6587\u7ae0\u6211\u5011\u8a2d\u5b9a\u597d iris \u6846\u67b6\u5f8c\u5148\u5c55\u793a\u4e00\u500b api \u7684\u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\uff0c\u9019\u7bc7\u6211\u5011\u8981\u7531\u4ecb\u7d39 iris \u6846\u67b6\u7684 MVC \u61c9\u7528\u7a0b\u5f0f\uff0c\u6240\u4ee5\u6211\u5011\u5c07\u7a0b\u5f0f\u9032\u5165\u9ede\u6539\u6210\u4ee5\u4e0b\u5167\u5bb9<\/p>\n<pre><code class=\"language-go\">package main\n\nimport (\n        &quot;github.com\/kataras\/iris&quot;\n        &quot;github.com\/kataras\/iris\/mvc&quot;\n    )\n\nfunc main() {\n    app := iris.New()\n    booksAPI := app.Party(&quot;\/books&quot;)\n    m := mvc.New(booksAPI)\n    app.Run(iris.Addr(&quot;:8080&quot;))\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u662f\u8a3b\u518a\u57fa\u672c\u7684url\u7684\u8def\u5f91\uff0c\u63a5\u4e0b\u4f86\u8aaa\u660e\u4e00\u4e0b <code>controller<\/code>\u7684\u5be6\u4f5c\u53ca\u7d81\u5b9a<\/p>\n<h3>controller \u7684\u5be6\u4f5c\u53ca\u7d81\u5b9a<\/h3>\n<p>\u4e0a\u9762\u7684\u4f8b\u5b50\u8a2d\u5b9a\u4e86 MVC \u7684 url\u6839\u76ee\u9304\u7684\u7d81\u5b9a\uff0c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39\u4e00\u4e0b<code>controller<\/code>\u7684\u7d81\u5b9a\uff0c\u8acb\u5927\u5bb6\u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">m.Handle(new(BookController))<\/code><\/pre>\n<p>\u5176\u4e2dBookController\u662f\u4e00\u500b\u7d50\u69cb\u5728\u4e4b\u5f8c\u53ef\u4ee5\u7528\u4f86\u8f49\u767c\u6216\u8655\u7406\u8acb\u6c42\u7684\u7d50\u69cb\u3002\u5176\u5167\u5bb9\u5982\u4e0b\u9762\u4f8b\u5b50<\/p>\n<pre><code class=\"language-go\">type BookController struct {\n    \/* dependencies *\/\n}<\/code><\/pre>\n<p>\u4ecb\u7d39\u5b8c<code>controller<\/code>\u4e4b\u5f8c\u8981\u4ecb\u7d39<code>action<\/code>\u7684\u7d81\u5b9a\u3002<\/p>\n<h3>action \u7684\u7d81\u5b9a<\/h3>\n<p>\u4e0a\u9762\u4ecb\u7d39\u5b8c<code>controller<\/code>\u7684\u5ba3\u544a\u4ee5\u53ca\u7d81\u5b9a\u4e4b\u5f8c\u63a5\u4e0b\u4f86\u8981\u4ecb\u7d39<code>controller<\/code>\u7684\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f<code>action<\/code>\u7684\u7d81\u5b9a\uff0c\u8acb\u5927\u5bb6\u8acb\u770b\u770b\u4e0b\u5217\u4f8b\u5b50\u3002<\/p>\n<pre><code class=\"language-go\">\/\/ GET: http:\/\/localhost:8080\/books\nfunc (c *BookController) Get() []Book {\n    return []Book{\n        {&quot;Mastering Concurrency in Go&quot;},\n        {&quot;Go Design Patterns&quot;},\n        {&quot;Black Hat Go&quot;},\n    }\n}\n\n\/\/ POST: http:\/\/localhost:8080\/books\nfunc (c *BookController) Post(b Book) int {\n    println(&quot;Received Book: &quot; + b.Title)\n\n    return iris.StatusCreated\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u4f8b\u5b50\u53cd\u6620\u4e86\u5982\u4f55\u7d81\u5b9a<code>action<\/code>\u4ee5\u53ca\u5c0d\u61c9\u7684url\u3002\u5176\u4e2d\u65b9\u6cd5\u540d\u7a31\u5c0d\u61c9\u7684\u5c31\u662f<code>HttpMethod<\/code>\u6240\u4ee5\u7c21\u55ae\u8b1b\u9664\u4e86\u4e0a\u8ff0\u7684\u4f8b\u5b50\u5916\u9084\u6709<code>delete<\/code>\u53ca<code>patch<\/code>\u7b49\u7b49\u7684\u65b9\u6cd5\u3002<br \/>\n\u9664\u6b64\u4e4b\u5916\u5982\u679c\u65b9\u6cd5\u540d\u7a31\u662f<code>GetBy<\/code>\u5c31\u53ef\u4ee5\u50b3\u5165\u53c3\u6578\uff0c\u9084\u6709\u4e00\u7a2e\u662f<code>GetLogin<\/code>\u7684\u65b9\u6cd5\u5c0d\u5370\u5230<code>GET:\/books\/login<\/code>\u7684url\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>\u9019\u4e00\u7bc7\u6587\u7ae0\u4ecb\u7d39 iris \u9019\u500b\u6846\u67b6\u7684 MVC \u7db2\u9801\u61c9\u7528\u7a0b\u5f0f\u8a72\u5982\u4f55\u5be6\u4f5c\uff0c\u4f46\u662f\u76ee\u524d\u53ea\u6709\u5be6\u4f5c <code>model<\/code> \u8ddf <code>controller<\/code> \u800c <code>view<\/code> \u5c07\u5728\u4e0b\u4e00\u7bc7\u8ddf\u5927\u5bb6\u4ecb\u7d39\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MVC \u5728\u914d\u7f6e\u597d iris \u7684\u5c08\u6848\u4e4b\u5f8c\uff0c\u63a5\u4e0b\u4f86\u5148\u4ecb\u7d39 MVC \u7684\u57fa\u672c\u6982\u5ff5\uff0c\u518d\u4f86\u4ecb\u7d39 iris \u7684 MVC \u7db2\u9801 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.develop-note.com\/blog\/2020\/10\/04\/iris-mvc-2021ironman\/\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Iris \u9019\u500b\u5728 go \u8a9e\u8a00\u4e0a\u5730\u8868\u6700\u5feb\u7684\u7db2\u9801\u6846\u67b6-iris\u7684mvc\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[158,81,84,86],"class_list":["post-1660","post","type-post","status-publish","format-standard","hentry","category-develop","tag-2020ironman","tag-go","tag-iris","tag-mvc"],"_links":{"self":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/comments?post=1660"}],"version-history":[{"count":14,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1660\/revisions"}],"predecessor-version":[{"id":2960,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/posts\/1660\/revisions\/2960"}],"wp:attachment":[{"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/media?parent=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/categories?post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.develop-note.com\/blog\/wp-json\/wp\/v2\/tags?post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}