{"id":1190,"date":"2025-01-23T23:56:44","date_gmt":"2025-01-23T15:56:44","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1190"},"modified":"2025-01-24T00:09:21","modified_gmt":"2025-01-23T16:09:21","slug":"yii-%e6%a0%b8%e5%bf%83%e5%8a%9f%e8%83%bd%e8%af%a6%e8%a7%a3%ef%bc%9a%e8%b7%af%e7%94%b1%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e3%80%81%e6%a8%a1%e5%9e%8b%e4%b8%8e%e8%a1%a8%e5%8d%95%e7%9a%84%e6%9c%80","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/23\/yii-%e6%a0%b8%e5%bf%83%e5%8a%9f%e8%83%bd%e8%af%a6%e8%a7%a3%ef%bc%9a%e8%b7%af%e7%94%b1%e3%80%81%e6%95%b0%e6%8d%ae%e5%ba%93%e3%80%81%e6%a8%a1%e5%9e%8b%e4%b8%8e%e8%a1%a8%e5%8d%95%e7%9a%84%e6%9c%80\/","title":{"rendered":"Yii \u6838\u5fc3\u529f\u80fd\u8be6\u89e3\uff1a\u8def\u7531\u3001\u6570\u636e\u5e93\u3001\u6a21\u578b\u4e0e\u8868\u5355\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/15\/yii\u6846\u67b6\u5168\u666f\u4ecb\u7ecd\uff1a\u4ece\u57fa\u7840\u5230\u8fdb\u9636\u7684\u5168\u9762\u89e3\u6790\/\">Yii\u6846\u67b6<\/a>\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u9ad8\u6548\u7684MVC\u6846\u67b6\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u529f\u80fd\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u5730\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8\u4e00\u4e9b\u6838\u5fc3\u6982\u5ff5\u548c\u529f\u80fd\uff0c\u5305\u62ec\u8def\u7531\u4e0eURL\u7ba1\u7406\u3001\u63a7\u5236\u5668\u4e0e\u89c6\u56fe\u3001\u6a21\u578b\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u4ee5\u53ca\u8868\u5355\u4e0e\u7528\u6237\u8f93\u5165\u5904\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>1. \u8def\u7531\u4e0e URL \u7ba1\u7406<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8def\u7531\u662fYii\u6846\u67b6\u4e2d\u5904\u7406\u8bf7\u6c42\u7684\u673a\u5236\u3002\u5b83\u5c06\u4e00\u4e2aURL\u6620\u5c04\u5230\u4e00\u4e2a\u63a7\u5236\u5668\u7684\u52a8\u4f5c\u3002Yii\u6846\u67b6\u5141\u8bb8\u4f60\u81ea\u5b9a\u4e49\u8def\u7531\u89c4\u5219\uff0c\u5e76\u4f7fURL\u66f4\u52a0\u7b80\u6d01\u3001\u7f8e\u89c2\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>1.1 \u8def\u7531\u89c4\u5219\u7684\u914d\u7f6e<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u8def\u7531\u662f\u6307URL\u6620\u5c04\u5230\u63a7\u5236\u5668\u548c\u52a8\u4f5c\u7684\u8def\u5f84\u3002\u5728Yii\u4e2d\uff0c\u9ed8\u8ba4\u7684\u8def\u7531\u89c4\u5219\u683c\u5f0f\u4e3a <code>\u63a7\u5236\u5668\/\u52a8\u4f5c<\/code>\uff0c\u4f8b\u5982 <code>site\/index<\/code> \u5bf9\u5e94 <code>SiteController<\/code> \u7684 <code>actionIndex<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u914d\u7f6e\u8def\u7531\u89c4\u5219<\/strong>\uff1a<br>\u5728 <code>config\/web.php<\/code> \u6587\u4ef6\u4e2d\u7684 <code>urlManager<\/code> \u914d\u7f6e\u4e2d\uff0c\u4f60\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8def\u7531\u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'components' =&gt; &#91;\n    'urlManager' =&gt; &#91;\n        'enablePrettyUrl' =&gt; true, \/\/ \u542f\u7528\u7f8e\u5316URL\n        'showScriptName' =&gt; false, \/\/ \u9690\u85cfindex.php\n        'rules' =&gt; &#91;\n            'post\/&lt;id:\\d+&gt;' =&gt; 'post\/view',  \/\/ \u6620\u5c04\u5230 post\/view \u52a8\u4f5c\n        ],\n    ],\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5f53\u8bf7\u6c42 URL \u4e3a <code>post\/123<\/code> \u65f6\uff0cYii \u4f1a\u5c06\u5176\u6620\u5c04\u5230 <code>PostController<\/code> \u7684 <code>actionView(123)<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>1.2 \u7f8e\u5316 URL \u7684\u5b9e\u73b0<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cYii \u7684 URL \u5305\u542b <code>index.php<\/code>\uff0c\u5982 <code>index.php?r=site\/index<\/code>\u3002\u8981\u5b9e\u73b0\u7f8e\u5316\u7684 URL\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 <code>config\/web.php<\/code> \u6587\u4ef6\u4e2d\u914d\u7f6e <code>enablePrettyUrl<\/code>\uff0c\u5e76\u901a\u8fc7 <code>.htaccess<\/code> \u6587\u4ef6\u6765\u53bb\u9664 <code>index.php<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>RewriteEngine on\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteCond %{REQUEST_FILENAME} !-d\nRewriteRule ^(.*)$ index.php\/$1 &#91;L]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\uff0cURL \u4f1a\u53d8\u6210\u7c7b\u4f3c <code>site\/index<\/code>\uff0c\u770b\u8d77\u6765\u66f4\u52a0\u7b80\u6d01\u548c\u53cb\u597d\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>1.3 RESTful \u8def\u7531\u8bbe\u7f6e<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Yii\u975e\u5e38\u9002\u5408\u6784\u5efaRESTful API\u3002\u8981\u8bbe\u7f6eRESTful\u8def\u7531\uff0c\u4f60\u53ef\u4ee5\u5728 <code>urlManager<\/code> \u7684\u89c4\u5219\u4e2d\u4f7f\u7528\u201c\u683c\u5f0f\u5316\u201d\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u8bbe\u7f6e\u4e00\u4e2a\u8d44\u6e90\u8def\u7531\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>'rules' =&gt; &#91;\n    'GET posts' =&gt; 'post\/index',\n    'GET post\/&lt;id:\\d+&gt;' =&gt; 'post\/view',\n    'POST post' =&gt; 'post\/create',\n    'PUT post\/&lt;id:\\d+&gt;' =&gt; 'post\/update',\n    'DELETE post\/&lt;id:\\d+&gt;' =&gt; 'post\/delete',\n],<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u89c4\u5219\uff0cYii\u4f1a\u81ea\u52a8\u5c06HTTP\u8bf7\u6c42\u65b9\u6cd5\u4e0e\u76f8\u5e94\u7684\u52a8\u4f5c\u6620\u5c04\u8d77\u6765\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>2. \u63a7\u5236\u5668\u4e0e\u89c6\u56fe<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a7\u5236\u5668\u548c\u89c6\u56fe\u662fYii\u6846\u67b6MVC\u6a21\u5f0f\u7684\u6838\u5fc3\u90e8\u5206\uff0c\u8d1f\u8d23\u5904\u7406\u8bf7\u6c42\u3001\u4e1a\u52a1\u903b\u8f91\u548c\u5c55\u793a\u6570\u636e\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>2.1 \u63a7\u5236\u5668\u7684\u751f\u547d\u5468\u671f<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a7\u5236\u5668\u662f\u5904\u7406\u8bf7\u6c42\u7684\u6838\u5fc3\u3002\u6bcf\u4e2a\u63a7\u5236\u5668\u90fd\u6709\u81ea\u5df1\u7684\u751f\u547d\u5468\u671f\uff0c\u5305\u62ec\u521d\u59cb\u5316\u3001\u6267\u884c\u52a8\u4f5c\u3001\u6e32\u67d3\u89c6\u56fe\u7b49\u3002\u63a7\u5236\u5668\u901a\u5e38\u7ee7\u627f\u81ea <code>yii\\web\\Controller<\/code> \u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u63a7\u5236\u5668\u7684\u521d\u59cb\u5316<\/strong>\uff1a<br>Yii\u6846\u67b6\u4f1a\u81ea\u52a8\u8c03\u7528 <code>init()<\/code> \u65b9\u6cd5\uff0c\u8fd9\u5bf9\u4e8e\u8bbe\u7f6e\u63a7\u5236\u5668\u7684\u521d\u59cb\u5316\u72b6\u6001\u975e\u5e38\u91cd\u8981\u3002\u4f60\u53ef\u4ee5\u91cd\u5199\u8be5\u65b9\u6cd5\u6765\u6267\u884c\u81ea\u5b9a\u4e49\u903b\u8f91\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SiteController extends yii\\web\\Controller\n{\n    public function init()\n    {\n        parent::init();\n        \/\/ \u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u4ee3\u7801\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>2.2 \u52a8\u4f5c\u7684\u521b\u5efa\u548c\u4f7f\u7528<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a7\u5236\u5668\u4e2d\u7684\u6bcf\u4e2a\u516c\u5171\u65b9\u6cd5\u90fd\u79f0\u4e3a\u201c\u52a8\u4f5c\u201d\u3002\u6bcf\u4e2a\u52a8\u4f5c\u90fd\u53ef\u4ee5\u901a\u8fc7 URL \u8def\u7531\u6765\u8bbf\u95ee\u3002\u4f8b\u5982\uff0c<code>actionIndex()<\/code> \u65b9\u6cd5\u901a\u5e38\u4f1a\u663e\u793a\u9996\u9875\u5185\u5bb9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class SiteController extends yii\\web\\Controller\n{\n    public function actionIndex()\n    {\n        return $this-&gt;render('index');\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>2.3 \u6570\u636e\u4f20\u9012\u4e0e\u6e32\u67d3\u89c6\u56fe<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u63a7\u5236\u5668\u901a\u8fc7 <code>render()<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u4f20\u9012\u5230\u89c6\u56fe\u3002<code>render()<\/code> \u65b9\u6cd5\u4f1a\u5c06\u6570\u636e\u4f20\u9012\u7ed9\u89c6\u56fe\u5e76\u8fd4\u56de\u6e32\u67d3\u540e\u7684\u5185\u5bb9\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u5f0f\u4f20\u9012\u6570\u636e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function actionIndex()\n{\n    return $this-&gt;render('index', &#91;\n        'name' =&gt; 'Yii2 Framework',\n    ]);\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>2.4 \u4f7f\u7528\u5e03\u5c40\u6587\u4ef6 (Layouts)<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e03\u5c40\u6587\u4ef6\u7528\u4e8e\u5b9a\u4e49\u9875\u9762\u7684\u516c\u5171\u90e8\u5206\uff0c\u5982\u5934\u90e8\u3001\u5e95\u90e8\u3001\u5bfc\u822a\u680f\u7b49\u3002\u5728 <code>views\/layouts<\/code> \u76ee\u5f55\u4e0b\u521b\u5efa\u5e03\u5c40\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7 <code>$this-&gt;beginLayout()<\/code> \u548c <code>$this-&gt;endLayout()<\/code> \u6765\u542f\u7528\u5e03\u5c40\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982\uff0c<code>views\/layouts\/main.php<\/code> \u6587\u4ef6\u53ef\u4ee5\u5305\u542b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;html&gt;\n    &lt;body&gt;\n        &lt;header&gt;Header content&lt;\/header&gt;\n        &lt;div class=\"content\"&gt;\n            &lt;?= $content ?&gt;\n        &lt;\/div&gt;\n        &lt;footer&gt;Footer content&lt;\/footer&gt;\n    &lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>3. \u6a21\u578b\u4e0e\u6570\u636e\u5e93\u64cd\u4f5c<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6a21\u578b\uff08Model\uff09\u662f\u5904\u7406\u6570\u636e\u7684\u6838\u5fc3\uff0c\u8d1f\u8d23\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u3002Yii\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684ActiveRecord\uff08AR\uff09\u548c\u67e5\u8be2\u6784\u9020\u5668\uff08QueryBuilder\uff09\u6765\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>3.1 \u6570\u636e\u6a21\u578b (Model) \u7684\u5b9a\u4e49<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728Yii\u4e2d\uff0c\u6a21\u578b\u7c7b\u901a\u5e38\u7ee7\u627f\u81ea <code>yii\\db\\ActiveRecord<\/code>\uff0c\u5e76\u4e0e\u6570\u636e\u5e93\u8868\u76f8\u5bf9\u5e94\u3002\u4f60\u53ef\u4ee5\u5728\u6a21\u578b\u4e2d\u5b9a\u4e49\u9a8c\u8bc1\u89c4\u5219\u3001\u5173\u7cfb\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Post extends yii\\db\\ActiveRecord\n{\n    public static function tableName()\n    {\n        return 'post';\n    }\n\n    public function rules()\n    {\n        return &#91;\n            &#91;&#91;'title', 'content'], 'required'],\n            &#91;'title', 'string', 'max' =&gt; 255],\n        ];\n    }\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>3.2 \u6570\u636e\u5e93\u8fc1\u79fb\u5de5\u5177<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u6570\u636e\u5e93\u8fc1\u79fb\u5de5\u5177\u7528\u4e8e\u7ba1\u7406\u6570\u636e\u5e93\u7ed3\u6784\u7684\u7248\u672c\u63a7\u5236\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u547d\u4ee4\u884c\u521b\u5efa\u8fc1\u79fb\u6587\u4ef6\u5e76\u5e94\u7528\u5b83\u4eec\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yii migrate\/create create_post_table<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5728\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\u4e2d\u5b9a\u4e49\u8868\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function safeUp()\n{\n    $this-&gt;createTable('post', &#91;\n        'id' =&gt; $this-&gt;primaryKey(),\n        'title' =&gt; $this-&gt;string(),\n        'content' =&gt; $this-&gt;text(),\n    ]);\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>3.3 Active Record (AR) \u7684\u4f7f\u7528<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Active Record \u4f7f\u5f97\u6570\u636e\u5e93\u7684\u589e\u5220\u6539\u67e5\uff08CRUD\uff09\u53d8\u5f97\u975e\u5e38\u7b80\u5355\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528 AR \u8fdb\u884c\u57fa\u672c\u64cd\u4f5c\u7684\u793a\u4f8b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u67e5\u8be2\u6240\u6709\u8bb0\u5f55<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $posts = Post::find()-&gt;all();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u63d2\u5165\u65b0\u8bb0\u5f55<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $post = new Post();\n  $post-&gt;title = 'New Post';\n  $post-&gt;content = 'Post content';\n  $post-&gt;save();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u66f4\u65b0\u8bb0\u5f55<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $post = Post::findOne(1);\n  $post-&gt;title = 'Updated Title';\n  $post-&gt;save();<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5220\u9664\u8bb0\u5f55<\/strong>\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $post = Post::findOne(1);\n  $post-&gt;delete();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>3.4 \u67e5\u8be2\u6784\u9020\u5668\uff08Query Builder\uff09<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Query Builder \u5141\u8bb8\u4f60\u4ee5\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\u6784\u9020\u67e5\u8be2\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$posts = Yii::$app-&gt;db-&gt;createCommand()\n    -&gt;select('*')\n    -&gt;from('post')\n    -&gt;where(&#91;'status' =&gt; 'active'])\n    -&gt;queryAll();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>3.5 \u6570\u636e\u9a8c\u8bc1\u4e0e\u8868\u5355\u8f93\u5165<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 Yii \u4e2d\uff0c\u6570\u636e\u9a8c\u8bc1\u901a\u5e38\u662f\u5728\u6a21\u578b\u7c7b\u4e2d\u8fdb\u884c\u7684\u3002\u4f8b\u5982\uff0c\u9a8c\u8bc1\u8868\u5355\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public function rules()\n{\n    return &#91;\n        &#91;&#91;'username', 'email'], 'required'],\n        &#91;'email', 'email'],\n        &#91;'username', 'string', 'max' =&gt; 255],\n    ];\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>4. \u8868\u5355\u4e0e\u7528\u6237\u8f93\u5165<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8868\u5355\u662f\u5904\u7406\u7528\u6237\u8f93\u5165\u7684\u5e38\u7528\u65b9\u5f0f\u3002Yii \u63d0\u4f9b\u4e86\u591a\u79cd\u8868\u5355\u9a8c\u8bc1\u89c4\u5219\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u6b63\u786e\u6027\u3002<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>4.1 \u521b\u5efa\u8868\u5355\u4e0e\u5904\u7406\u7528\u6237\u8f93\u5165<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u6a21\u578b\u6765\u5904\u7406\u8868\u5355\u8f93\u5165\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$model = new Post();\nif ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;validate()) {\n    $model-&gt;save();\n} else {\n    return $this-&gt;render('create', &#91;'model' =&gt; $model]);\n}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>4.2 \u8868\u5355\u9a8c\u8bc1\u89c4\u5219<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Yii\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u9a8c\u8bc1\u89c4\u5219\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u8fdb\u884c\u8bbe\u7f6e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>required<\/code>\uff1a\u9a8c\u8bc1\u5b57\u6bb5\u662f\u5426\u4e3a\u7a7a\u3002<\/li>\n\n\n\n<li><code>email<\/code>\uff1a\u9a8c\u8bc1\u7535\u5b50\u90ae\u4ef6\u683c\u5f0f\u3002<\/li>\n\n\n\n<li><code>string<\/code>\uff1a\u9a8c\u8bc1\u5b57\u7b26\u4e32\u957f\u5ea6\u3002<\/li>\n\n\n\n<li><code>integer<\/code>\uff1a\u9a8c\u8bc1\u662f\u5426\u4e3a\u6574\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>4.3 \u4f7f\u7528 Yii \u63d0\u4f9b\u7684 HTML \u8f85\u52a9\u7c7b<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Yii \u63d0\u4f9b\u4e86 <code>yii\\helpers\\Html<\/code> \u7c7b\u6765\u5e2e\u52a9\u751f\u6210\u5b89\u5168\u7684HTML\u5143\u7d20\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?= Html::encode($post-&gt;title) ?&gt;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>4.4 \u9632\u6b62\u8de8\u7ad9\u811a\u672c\u653b\u51fb (XSS) \u548c SQL \u6ce8\u5165<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Yii \u9ed8\u8ba4\u4f7f\u7528 <code>Html::encode()<\/code> \u548c <code>Yii::t()<\/code> \u6765\u9632\u6b62 XSS \u653b\u51fb\u3002\u800c\u5728\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\uff0cActiveRecord \u548c QueryBuilder \u81ea\u52a8\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\uff0c\u9632\u6b62 SQL \u6ce8\u5165\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u9632\u6b62XSS\n&lt;?= Html\n\n::encode($content) ?&gt;\n\n\/\/ \u9632\u6b62SQL\u6ce8\u5165\n$posts = Post::find()-&gt;where(&#91;'status' =&gt; 'active'])-&gt;all();<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u8fd9\u4e9b\u6838\u5fc3\u6982\u5ff5\u548c\u529f\u80fd\uff0c\u4f60\u5c06\u80fd\u591f\u4f7f\u7528 Yii \u6846\u67b6\u5feb\u901f\u6784\u5efa\u73b0\u4ee3\u7684 Web \u5e94\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yii\u6846\u67b6\u4e0d\u4ec5\u4ec5\u662f\u4e00\u4e2a\u9ad8\u6548\u7684MVC\u6846\u67b6\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u529f\u80fd\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u9ad8\u6548\u5730\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5728\u8fd9\u4e00\u7ae0\u8282\u4e2d\uff0c\u6211 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[63],"tags":[],"class_list":["post-1190","post","type-post","status-publish","format-standard","hentry","category-yii"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1190"}],"version-history":[{"count":2,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1190\/revisions"}],"predecessor-version":[{"id":1205,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1190\/revisions\/1205"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}