{"id":183852,"date":"2024-11-10T08:16:20","date_gmt":"2024-11-10T00:16:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/183852\/"},"modified":"2024-11-10T08:16:21","modified_gmt":"2024-11-10T00:16:21","slug":"%e4%bd%bf%e7%94%a8mvc%e6%a1%86%e6%9e%b6%e5%8b%95%e6%85%8b%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b7%bb%e5%8a%a0-mvc-%e5%8b%95%e6%85%8b%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/183852\/","title":{"rendered":"\u4f7f\u7528MVC\u6846\u67b6\u52d5\u614b\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u6dfb\u52a0 (mvc \u52d5\u614b\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5)"},"content":{"rendered":"<h1 id=\"%e4%bd%bf%e7%94%a8mvc%e6%a1%86%e6%9e%b6%e5%8b%95%e6%85%8b%e5%af%a6%e7%8f%be%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e6%b7%bb%e5%8a%a0-ymJYijUDNJ\">\u4f7f\u7528MVC\u6846\u67b6\u52d5\u614b\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u6dfb\u52a0<\/h1>\n<p>\u5728\u73fe\u4ee3\u7684\u7db2\u9801\u958b\u767c\u4e2d\uff0cMVC\uff08Model-View-Controller\uff09\u6846\u67b6\u5df2\u7d93\u6210\u70ba\u4e00\u7a2e\u6d41\u884c\u7684\u67b6\u69cb\u6a21\u5f0f\u3002\u5b83\u5c07\u61c9\u7528\u7a0b\u5e8f\u7684\u908f\u8f2f\u5206\u70ba\u4e09\u500b\u4e3b\u8981\u90e8\u5206\uff0c\u5f9e\u800c\u4f7f\u5f97\u958b\u767c\u904e\u7a0b\u66f4\u52a0\u6e05\u6670\u548c\u9ad8\u6548\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u5982\u4f55\u5728MVC\u6846\u67b6\u4e2d\u52d5\u614b\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u7684\u6dfb\u52a0\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u5be6\u7528\u7684\u793a\u4f8b\u548c\u4ee3\u78bc\u7247\u6bb5\u3002<\/p>\n<h2 id=\"mvc%e6%9e%b6%e6%a7%8b%e6%a6%82%e8%bf%b0-ymJYijUDNJ\">MVC\u67b6\u69cb\u6982\u8ff0<\/h2>\n<p>MVC\u67b6\u69cb\u7684\u4e3b\u8981\u7d44\u6210\u90e8\u5206\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>Model\uff1a<\/strong>\u8ca0\u8cac\u6578\u64da\u548c\u696d\u52d9\u908f\u8f2f\u7684\u8655\u7406\u3002<\/li>\n<li><strong>View\uff1a<\/strong>\u8ca0\u8cac\u7528\u6236\u754c\u9762\u7684\u986f\u793a\u3002<\/li>\n<li><strong>Controller\uff1a<\/strong>\u8ca0\u8cac\u63a5\u6536\u7528\u6236\u8f38\u5165\u4e26\u8abf\u7528\u76f8\u61c9\u7684Model\u548cView\u3002<\/li>\n<\/ul>\n<p>\u9019\u7a2e\u5206\u96e2\u7684\u8a2d\u8a08\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u5bb9\u6613\u5730\u7ba1\u7406\u4ee3\u78bc\uff0c\u4e26\u4e14\u5728\u9700\u8981\u6642\u53ef\u4ee5\u7368\u7acb\u5730\u4fee\u6539\u67d0\u4e00\u90e8\u5206\u800c\u4e0d\u5f71\u97ff\u5176\u4ed6\u90e8\u5206\u3002<\/p>\n<h2 id=\"%e5%8b%95%e6%85%8b%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5%e7%9a%84%e9%9c%80%e6%b1%82-ymJYijUDNJ\">\u52d5\u614b\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5\u7684\u9700\u6c42<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u958b\u767c\u8005\u53ef\u80fd\u9700\u8981\u6839\u64da\u7528\u6236\u7684\u9700\u6c42\u52d5\u614b\u5730\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5\u3002\u4f8b\u5982\uff0c\u7576\u7528\u6236\u9700\u8981\u9023\u63a5\u5230\u4e0d\u540c\u7684\u6578\u64da\u5eab\u6642\uff0c\u9019\u7a2e\u9700\u6c42\u5c31\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002\u9019\u6a23\u7684\u529f\u80fd\u53ef\u4ee5\u63d0\u9ad8\u61c9\u7528\u7a0b\u5e8f\u7684\u9748\u6d3b\u6027\u548c\u53ef\u64f4\u5c55\u6027\u3002<\/p>\n<h2 id=\"%e5%af%a6%e7%8f%be%e6%ad%a5%e9%a9%9f-ymJYijUDNJ\">\u5be6\u73fe\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e9%85%8d%e7%bd%ae%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-ymJYijUDNJ\">1. \u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5728MVC\u6846\u67b6\u4e2d\u914d\u7f6e\u6578\u64da\u5eab\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u4f7f\u7528PHP\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nclass Database {\n    private $host;\n    private $db_name;\n    private $username;\n    private $password;\n    public $conn;\n\n    public function __construct($host, $db_name, $username, $password) {\n        $this-&gt;host = $host;\n        $this-&gt;db_name = $db_name;\n        $this-&gt;username = $username;\n        $this-&gt;password = $password;\n        $this-&gt;connect();\n    }\n\n    public function connect() {\n        $this-&gt;conn = null;\n        try {\n            $this-&gt;conn = new PDO(\"mysql:host={$this-&gt;host};dbname={$this-&gt;db_name}\", $this-&gt;username, $this-&gt;password);\n            $this-&gt;conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\n        } catch(PDOException $exception) {\n            echo \"Connection error: \" . $exception-&gt;getMessage();\n        }\n    }\n}\n<\/code><\/pre>\n<h3 id=\"2-%e5%8b%95%e6%85%8b%e6%b7%bb%e5%8a%a0%e6%95%b8%e6%93%9a%e5%ba%ab%e9%80%a3%e6%8e%a5-ymJYijUDNJ\">2. \u52d5\u614b\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u53ef\u4ee5\u5275\u5efa\u4e00\u500b\u65b9\u6cd5\u4f86\u52d5\u614b\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5\u3002\u9019\u500b\u65b9\u6cd5\u53ef\u4ee5\u63a5\u6536\u7528\u6236\u8f38\u5165\u7684\u6578\u64da\u5eab\u53c3\u6578\uff0c\u4e26\u5275\u5efa\u65b0\u7684\u6578\u64da\u5eab\u9023\u63a5\u3002<\/p>\n<pre><code>\nclass DatabaseManager {\n    private $connections = [];\n\n    public function addConnection($host, $db_name, $username, $password) {\n        $db = new Database($host, $db_name, $username, $password);\n        $this-&gt;connections[] = $db;\n    }\n\n    public function getConnections() {\n        return $this-&gt;connections;\n    }\n}\n<\/code><\/pre>\n<h3 id=\"3-%e5%9c%a8controller%e4%b8%ad%e4%bd%bf%e7%94%a8-ymJYijUDNJ\">3. \u5728Controller\u4e2d\u4f7f\u7528<\/h3>\n<p>\u6700\u5f8c\uff0c\u6211\u5011\u9700\u8981\u5728Controller\u4e2d\u4f7f\u7528\u9019\u500bDatabaseManager\u4f86\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>\nclass DatabaseController {\n    private $dbManager;\n\n    public function __construct() {\n        $this-&gt;dbManager = new DatabaseManager();\n    }\n\n    public function addDatabaseConnection($host, $db_name, $username, $password) {\n        $this-&gt;dbManager-&gt;addConnection($host, $db_name, $username, $password);\n    }\n\n    public function listConnections() {\n        return $this-&gt;dbManager-&gt;getConnections();\n    }\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-ymJYijUDNJ\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u6211\u5011\u53ef\u4ee5\u5728MVC\u6846\u67b6\u4e2d\u52d5\u614b\u5730\u6dfb\u52a0\u6578\u64da\u5eab\u9023\u63a5\u3002\u9019\u7a2e\u65b9\u6cd5\u4e0d\u50c5\u63d0\u9ad8\u4e86\u61c9\u7528\u7a0b\u5e8f\u7684\u9748\u6d3b\u6027\uff0c\u9084\u4f7f\u5f97\u6578\u64da\u5eab\u7ba1\u7406\u8b8a\u5f97\u66f4\u52a0\u7c21\u55ae\u3002\u96a8\u8457\u9700\u6c42\u7684\u8b8a\u5316\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u64f4\u5c55\u548c\u4fee\u6539\u6578\u64da\u5eab\u9023\u63a5\u7684\u914d\u7f6e\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u9ad8\u6548\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u60a8\u7684\u61c9\u7528\u7a0b\u5e8f\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u9084\u662f\u5176\u4ed6\u985e\u578b\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u7a69\u5b9a\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528MVC\u6846\u67b6\u52d5\u614b\u5be6\u73fe\u6578\u64da\u5eab\u9023\u63a5\u6dfb\u52a0\uff0c\u7c21\u5316\u6578\u64da\u7ba1\u7406\u6d41\u7a0b\uff0c\u63d0\u5347\u61c9\u7528\u9748\u6d3b\u6027\u8207\u64f4\u5c55\u6027\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-183852","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=183852"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183852\/revisions"}],"predecessor-version":[{"id":183853,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/183852\/revisions\/183853"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=183852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=183852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=183852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}