{"id":23191,"date":"2024-11-21T11:29:23","date_gmt":"2024-11-21T03:29:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23191\/"},"modified":"2024-11-21T11:29:23","modified_gmt":"2024-11-21T03:29:23","slug":"thinkphp-fetch%e6%96%b9%e6%b3%95%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23191\/","title":{"rendered":"thinkphp fetch\u65b9\u6cd5\u600e\u4e48\u7528"},"content":{"rendered":"<\/p>\n<p>Fetch method in ThinkPHP framework is a rendering method that is primarily used to load view pages and render them.\u3002\u9996\u5148\u8981\u627e\u5230ThinkPHP\u6846\u67b6\u4e2d\u7684View\u7c7b\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49\u7684\u3002<\/p>\n<p>View\u7c7b\u5728ThinkPHP\u6846\u67b6\u4e2d\u7684\u8def\u5f84\u5982\u4e0b\uff1a<\/p>\n<pre>thinkphp\/library\/think\/View.php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u8def\u5f84\u627e\u5230View\u7c7b\u6240\u5728\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u5728View.php\u6e90\u6587\u4ef6\u4e2d\uff0c\u6709\u4e00\u4e2aView\u7c7b\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86fetch()\u65b9\u6cd5\u7684\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>\/**\n&nbsp;*&nbsp;\u6e32\u67d3\u6a21\u677f\u8f93\u51fa\n&nbsp;*&nbsp;@access&nbsp;public\n&nbsp;*&nbsp;@param&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;$templateFile&nbsp;\u6a21\u677f\u6587\u4ef6\u540d\n&nbsp;*&nbsp;@param&nbsp;array&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$vars&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u6a21\u677f\u8f93\u51fa\u53d8\u91cf\n&nbsp;*&nbsp;@param&nbsp;array&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$config&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u6a21\u677f\u53c2\u6570\n&nbsp;*&nbsp;@return&nbsp;void\n&nbsp;*&nbsp;@throws&nbsp;Exception\n&nbsp;*\/\npublic&nbsp;function&nbsp;fetch($templateFile&nbsp;=&nbsp;'',&nbsp;$vars&nbsp;=&nbsp;[],&nbsp;$config&nbsp;=&nbsp;[])\n{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5c06\u53d8\u91cf\u8d4b\u503c\u5230\u89c6\u56fe\u6a21\u677f\u4e2d\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!empty($vars))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&amp;gt;assign($vars);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406\u6a21\u677f\u6587\u4ef6\u540d\u5e76\u5224\u65ad\u662f\u5426\u5b58\u5728\n&nbsp;&nbsp;&nbsp;&nbsp;$templateFile&nbsp;=&nbsp;$this-&amp;gt;parseTemplateFile($templateFile);\n\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!is_file($templateFile))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception('template&nbsp;file&nbsp;not&nbsp;exists:'&nbsp;.&nbsp;$templateFile);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u6a21\u677f\u8f93\u51fa\u8fc7\u6ee4\n&nbsp;&nbsp;&nbsp;&nbsp;$this-&amp;gt;filter($templateFile);\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u89e3\u6790\u89c6\u56fe\u6a21\u677f\u4e2d\u7684\u51fd\u6570\n&nbsp;&nbsp;&nbsp;&nbsp;$content&nbsp;=&nbsp;$this-&amp;gt;fetchParse($templateFile,&nbsp;$config);\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u89c6\u56fe\u6a21\u677f\u7f16\u8bd1\u7f13\u5b58\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&amp;gt;config('tpl_cache')&nbsp;&amp;amp;&amp;amp;&nbsp;!empty($TemplateCache))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$TemplateCache-&amp;gt;set($cacheFile,&nbsp;$content);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8fd4\u56de\u89e3\u6790\u540e\u7684\u89c6\u56fe\u6a21\u677f\u5185\u5bb9\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$content;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230fetch\u65b9\u6cd5\u7684\u5b9a\u4e49\u548c\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728fetch\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4f7f\u7528assign\u65b9\u6cd5\u4f20\u9012\u6a21\u677f\u53d8\u91cf\u548c\u8981\u6e32\u67d3\u7684\u6a21\u677f\u6587\u4ef6\u540d\uff0c\u4ee5\u8fdb\u884c\u53d8\u91cf\u8d4b\u503c\u3002\u63a5\u7740\u5224\u65ad\u6a21\u677f\u6587\u4ef6\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\u5219\u629b\u51fa\u5f02\u5e38\u3002\u6700\u540e\uff0c\u8fdb\u884c\u89c6\u56fe\u6a21\u677f\u8f93\u51fa\u8fc7\u6ee4\uff0c\u89e3\u6790\u89c6\u56fe\u6a21\u677f\u4e2d\u7684\u51fd\u6570\u5e76\u8fd4\u56de\u5904\u7406\u540e\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f fetch\u65b9\u6cd5\u600e\u4e48\u7528\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>Fetch method in ThinkPHP framework is a rendering method that is primarily used to load view pages and render them.\u3002\u9996\u5148\u8981\u627e\u5230ThinkPHP\u6846\u67b6\u4e2d\u7684View\u7c7b\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u65b9\u6cd5\u662f\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49\u7684\u3002 View\u7c7b\u5728ThinkPHP\u6846\u67b6\u4e2d\u7684\u8def\u5f84\u5982\u4e0b\uff1a thinkphp\/library\/think\/View.php \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u8def\u5f84\u627e\u5230View\u7c7b\u6240\u5728\u7684\u6e90\u4ee3\u7801\u6587\u4ef6\u3002\u5728View.php\u6e90\u6587\u4ef6\u4e2d\uff0c\u6709\u4e00\u4e2aView\u7c7b\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86fetch()\u65b9\u6cd5\u7684\u4ee3\u7801\uff0c\u5982\u4e0b\u6240\u793a\uff1a \/** &nbsp;*&nbsp;\u6e32\u67d3\u6a21\u677f\u8f93\u51fa &nbsp;*&nbsp;@access&nbsp;public &nbsp;*&nbsp;@param&nbsp;string&nbsp;&nbsp;&nbsp;&nbsp;$templateFile&nbsp;\u6a21\u677f\u6587\u4ef6\u540d &nbsp;*&nbsp;@param&nbsp;array&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$vars&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u6a21\u677f\u8f93\u51fa\u53d8\u91cf &nbsp;*&nbsp;@param&nbsp;array&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$config&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u6a21\u677f\u53c2\u6570 &nbsp;*&nbsp;@return&nbsp;void &nbsp;*&nbsp;@throws&nbsp;Exception &nbsp;*\/ public&nbsp;function&nbsp;fetch($templateFile&nbsp;=&nbsp;&#8221;,&nbsp;$vars&nbsp;=&nbsp;[],&nbsp;$config&nbsp;=&nbsp;[]) { &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5c06\u53d8\u91cf\u8d4b\u503c\u5230\u89c6\u56fe\u6a21\u677f\u4e2d &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!empty($vars))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this-&amp;gt;assign($vars); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5904\u7406\u6a21\u677f\u6587\u4ef6\u540d\u5e76\u5224\u65ad\u662f\u5426\u5b58\u5728 &nbsp;&nbsp;&nbsp;&nbsp;$templateFile&nbsp;=&nbsp;$this-&amp;gt;parseTemplateFile($templateFile); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!is_file($templateFile))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw&nbsp;new&nbsp;Exception(&#8216;template&nbsp;file&nbsp;not&nbsp;exists:&#8217;&nbsp;.&nbsp;$templateFile); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u6a21\u677f\u8f93\u51fa\u8fc7\u6ee4 &nbsp;&nbsp;&nbsp;&nbsp;$this-&amp;gt;filter($templateFile); &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u89e3\u6790\u89c6\u56fe\u6a21\u677f\u4e2d\u7684\u51fd\u6570 &nbsp;&nbsp;&nbsp;&nbsp;$content&nbsp;=&nbsp;$this-&amp;gt;fetchParse($templateFile,&nbsp;$config); &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u89c6\u56fe\u6a21\u677f\u7f16\u8bd1\u7f13\u5b58 &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($this-&amp;gt;config(&#8216;tpl_cache&#8217;)&nbsp;&amp;amp;&amp;amp;&nbsp;!empty($TemplateCache))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$TemplateCache-&amp;gt;set($cacheFile,&nbsp;$content); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8fd4\u56de\u89e3\u6790\u540e\u7684\u89c6\u56fe\u6a21\u677f\u5185\u5bb9 &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;$content; } [&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-23191","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23191","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=23191"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23191\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}