{"id":22974,"date":"2024-11-21T11:52:16","date_gmt":"2024-11-21T03:52:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22974\/"},"modified":"2024-11-21T11:52:16","modified_gmt":"2024-11-21T03:52:16","slug":"thinkphp%e9%85%8d%e7%bd%ae%e6%96%87%e4%bb%b6%e4%bf%ae%e6%94%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22974\/","title":{"rendered":"thinkphp\u914d\u7f6e\u6587\u4ef6\u4fee\u6539"},"content":{"rendered":"<p>\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u5f00\u53d1php\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u4fee\u6539\u4ee5\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4fee\u6539thinkphp\u7684\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<ol>\n<li>\u627e\u5230\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<p>ThinkPHP\u7684\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u5b58\u653e\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u7684application\u76ee\u5f55\u4e2d\u7684config.php\u6587\u4ef6\u4e2d\u3002\u4e5f\u6709\u53ef\u80fd\u5b58\u5728database.php\u7b49\u5176\u4ed6\u914d\u7f6e\u6587\u4ef6\uff0c\u4e0d\u8fc7\u8fd9\u4e9b\u6587\u4ef6\u7684\u540d\u79f0\u548c\u4f4d\u7f6e\u53ef\u80fd\u4f1a\u56e0\u4e2a\u4eba\u6216\u516c\u53f8\u5f00\u53d1\u8005\u7684\u4e60\u60ef\u800c\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<ol>\n<li>\u6253\u5f00\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5176\u4e2d\u5305\u542b\u4e86\u5927\u91cf\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u6309\u7167\u4e0d\u540c\u7684\u529f\u80fd\u5206\u4e3a\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u5982\u6570\u636e\u5e93\u3001\u8def\u7531\u3001\u7f13\u5b58\u3001\u65e5\u5fd7\u7b49\u3002<\/p>\n<p>\u5bf9\u4e8e\u6307\u5b9a\u7684\u914d\u7f6e\u9879\uff0c\u53ef\u4ee5\u6309\u7167\u60ef\u4f8b\u627e\u5230\u5bf9\u5e94\u7684\u914d\u7f6e\u9879\u5e76\u4fee\u6539\u5176\u503c\u3002\u6bd4\u5982\uff1a<\/p>\n<pre>return [\n\n    \/\/ \u6570\u636e\u5e93\u914d\u7f6e\n    'database' =&amp;gt; [\n        'type'        =&amp;gt; 'mysql',\n        'hostname'    =&amp;gt; 'localhost',\n        'database'    =&amp;gt; 'test',\n        'username'    =&amp;gt; 'root',\n        'password'    =&amp;gt; '123456',\n        'hostport'    =&amp;gt; '3306',\n        'charset'     =&amp;gt; 'utf8', \n        'prefix'      =&amp;gt; '',\n        'debug'       =&amp;gt; true,\n        'deploy'      =&amp;gt; 0,\n        'rw_separate' =&amp;gt; false,\n        'master_num'  =&amp;gt; 1,\n        'slave_no'    =&amp;gt; '',\n        'fields_strict' =&amp;gt; true,\n        'resultset_type' =&amp;gt; 'array',\n        'auto_timestamp' =&amp;gt; false,\n        'sql_explain' =&amp;gt; false,\n    ],\n\n    \/\/ \u8def\u7531\u914d\u7f6e\n    'route' =&amp;gt; [\n        'default_controller'    =&amp;gt; 'Index',\n        'default_action'        =&amp;gt; 'index',\n        'default_module'        =&amp;gt; 'index',\n        'url_html_suffix'       =&amp;gt; 'html',\n        'url_common_param'      =&amp;gt; true, \n        'url_route_on'          =&amp;gt; true, \n        'route_complete_match'  =&amp;gt; false,\n        'url_route_must'        =&amp;gt; false,\n        'url_domain_deploy'     =&amp;gt; false,\n        'url_domain_root'       =&amp;gt; '', \n        'url_convert'           =&amp;gt; false, \n        'url_controller_layer'  =&amp;gt; 'controller',\n        'var_controller'        =&amp;gt; 'c',\n        'var_action'            =&amp;gt; 'a',\n    ],\n\n    \/\/ \u7f13\u5b58\u914d\u7f6e\n    'cache' =&amp;gt; [\n        'type'   =&amp;gt; 'File',\n        'expire' =&amp;gt; 0,\n        'prefix' =&amp;gt; '',\n        'path'   =&amp;gt; '',\n        'host'   =&amp;gt; '',\n        'port'   =&amp;gt; '',\n        'password' =&amp;gt; '',\n        'select' =&amp;gt; 0,\n        'persistent' =&amp;gt; false,\n        'timeout' =&amp;gt; 0,\n        'persistent_id' =&amp;gt; '',\n    ],\n\n    \/\/ \u65e5\u5fd7\u914d\u7f6e\n    'log' =&amp;gt; [\n        'type' =&amp;gt; 'File',\n        'path' =&amp;gt; LOG_PATH,\n        'level' =&amp;gt; ['error'],\n    ],\n\n    \/\/ \u5176\u4ed6\u914d\u7f6e...\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u5e0c\u671b\u5c06\u6570\u636e\u5e93\u5bc6\u7801\u6539\u4e3a654321\uff0c\u53ea\u9700\u5728\u5bf9\u5e94\u7684\u914d\u7f6e\u9879\u4e2d\u4fee\u6539\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>'database' =&amp;gt; [ \n    'type'        =&amp;gt; 'mysql',\n    'hostname'    =&amp;gt; 'localhost',\n    'database'    =&amp;gt; 'test',\n    'username'    =&amp;gt; 'root',\n    'password'    =&amp;gt; '654321', \/\/ \u5c06password\u503c\u4fee\u6539\u4e3a\u65b0\u5bc6\u7801\n    'hostport'    =&amp;gt; '3306',\n    'charset'     =&amp;gt; 'utf8', \n    'prefix'      =&amp;gt; '',\n    'debug'       =&amp;gt; true,\n    'deploy'      =&amp;gt; 0,\n    'rw_separate' =&amp;gt; false,\n    'master_num'  =&amp;gt; 1,\n    'slave_no'    =&amp;gt; '',\n    'fields_strict' =&amp;gt; true,\n    'resultset_type' =&amp;gt; 'array',\n    'auto_timestamp' =&amp;gt; false,\n    'sql_explain' =&amp;gt; false,\n],<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u4fdd\u5b58\u4fee\u6539<\/li>\n<\/ol>\n<p>\u4fee\u6539\u5b8c\u6bd5\u540e\uff0c\u76f4\u63a5\u4fdd\u5b58\u5373\u53ef\u3002<\/p>\n<ol>\n<li>\u6d4b\u8bd5\u4fee\u6539<\/li>\n<\/ol>\n<p>\u4e3a\u786e\u4fdd\u4fee\u6539\u751f\u6548\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5c1d\u8bd5\u8bfb\u53d6\u4fee\u6539\u540e\u7684\u914d\u7f6e\u503c\u3002\u6bd4\u5982\uff0c\u5728\u4e00\u4e2a\u63a7\u5236\u5668\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u8bfb\u53d6\u6570\u636e\u5e93\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\uff1a<\/p>\n<pre>&lt;?php namespace appindexcontroller;\n\nclass Test\n{\n    public function index()\n    {\n        $config = config('database'); \/\/ \u83b7\u53d6\u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f\n        echo '\u7528\u6237\u540d\uff1a'. $config['username'] .'&lt;br&gt;';\n        echo '\u5bc6\u7801\uff1a'. $config['password'] .'&lt;br&gt;';\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee\u8be5\u63a7\u5236\u5668\u7684\u65b9\u6cd5\uff0c\u5373\u53ef\u770b\u5230\u8f93\u51fa\u7684\u7528\u6237\u540d\u548c\u5bc6\u7801\u5df2\u7ecf\u88ab\u4fee\u6539\u4e3a\u65b0\u503c\u3002<\/p>\n<p>\u901a\u8fc7\u4fee\u6539ThinkPHP\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u5730\u8c03\u6574\u5e94\u7528\u7a0b\u5e8f\u7684\u5404\u79cd\u914d\u7f6e\u53c2\u6570\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u9002\u5e94\u4e0d\u540c\u7684\u4e1a\u52a1\u9700\u6c42\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u5e94\u8be5\u6839\u636e\u5177\u4f53\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u914d\u7f6e\u53c2\u6570\u8fdb\u884c\u4fee\u6539\uff0c\u4ee5\u5145\u5206\u53d1\u6325\u6846\u67b6\u7684\u4f18\u52bf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u914d\u7f6e\u6587\u4ef6\u4fee\u6539\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u4f7f\u7528thinkphp\u6846\u67b6\u5f00\u53d1php\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u7ecf\u5e38\u9700\u8981\u5bf9\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u4fee\u6539\u4ee5\u6ee1\u8db3\u4e1a\u52a1\u9700\u6c42\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4fee\u6539thinkphp\u7684\u914d\u7f6e\u6587\u4ef6\u3002 \u627e\u5230\u914d\u7f6e\u6587\u4ef6 ThinkPHP\u7684\u914d\u7f6e\u6587\u4ef6\u901a\u5e38\u5b58\u653e\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u7684application\u76ee\u5f55\u4e2d\u7684config.php\u6587\u4ef6\u4e2d\u3002\u4e5f\u6709\u53ef\u80fd\u5b58\u5728database.php\u7b49\u5176\u4ed6\u914d\u7f6e\u6587\u4ef6\uff0c\u4e0d\u8fc7\u8fd9\u4e9b\u6587\u4ef6\u7684\u540d\u79f0\u548c\u4f4d\u7f6e\u53ef\u80fd\u4f1a\u56e0\u4e2a\u4eba\u6216\u516c\u53f8\u5f00\u53d1\u8005\u7684\u4e60\u60ef\u800c\u6709\u6240\u4e0d\u540c\u3002 \u6253\u5f00\u914d\u7f6e\u6587\u4ef6 \u901a\u8fc7\u6253\u5f00\u8be5\u6587\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5176\u4e2d\u5305\u542b\u4e86\u5927\u91cf\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u8fd9\u4e9b\u914d\u7f6e\u4fe1\u606f\u6309\u7167\u4e0d\u540c\u7684\u529f\u80fd\u5206\u4e3a\u4e0d\u540c\u7684\u6a21\u5757\uff0c\u5982\u6570\u636e\u5e93\u3001\u8def\u7531\u3001\u7f13\u5b58\u3001\u65e5\u5fd7\u7b49\u3002 \u5bf9\u4e8e\u6307\u5b9a\u7684\u914d\u7f6e\u9879\uff0c\u53ef\u4ee5\u6309\u7167\u60ef\u4f8b\u627e\u5230\u5bf9\u5e94\u7684\u914d\u7f6e\u9879\u5e76\u4fee\u6539\u5176\u503c\u3002\u6bd4\u5982\uff1a return [ \/\/ \u6570\u636e\u5e93\u914d\u7f6e &#8216;database&#8217; =&amp;gt; [ &#8216;type&#8217; =&amp;gt; &#8216;mysql&#8217;, &#8216;hostname&#8217; =&amp;gt; &#8216;localhost&#8217;, &#8216;database&#8217; =&amp;gt; &#8216;test&#8217;, &#8216;username&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; &#8216;123456&#8217;, &#8216;hostport&#8217; =&amp;gt; &#8216;3306&#8217;, &#8216;charset&#8217; =&amp;gt; &#8216;utf8&#8217;, &#8216;prefix&#8217; =&amp;gt; &#8221;, &#8216;debug&#8217; =&amp;gt; true, &#8216;deploy&#8217; =&amp;gt; 0, &#8216;rw_separate&#8217; =&amp;gt; false, &#8216;master_num&#8217; =&amp;gt; 1, &#8216;slave_no&#8217; =&amp;gt; &#8221;, &#8216;fields_strict&#8217; =&amp;gt; true, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22974","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22974","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22974"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22974\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}