{"id":23865,"date":"2024-11-21T09:48:37","date_gmt":"2024-11-21T01:48:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23865\/"},"modified":"2024-11-21T09:48:37","modified_gmt":"2024-11-21T01:48:37","slug":"%e4%bd%bf%e7%94%a8yii%e6%a1%86%e6%9e%b6%e5%88%9b%e5%bb%ba%e5%9c%a8%e7%ba%bf%e6%95%99%e8%82%b2%e7%bd%91%e7%ab%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23865\/","title":{"rendered":"\u4f7f\u7528Yii\u6846\u67b6\u521b\u5efa\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u666e\u53ca\u548c\u4e92\u8054\u7f51\u7528\u6237\u7684\u589e\u52a0\uff0c\u6559\u80b2\u884c\u4e1a\u4e5f\u4e0d\u65ad\u5730\u5411\u7ebf\u4e0a\u8f6c\u79fb\uff0c\u6253\u9020\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\u5df2\u7ecf\u6210\u4e3a\u4e86\u73b0\u4ee3\u6559\u80b2\u884c\u4e1a\u7684\u8d8b\u52bf\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u8d8b\u52bf\uff0c\u9009\u62e9\u4e00\u4e2a\u9ad8\u6548\u7684\u6846\u67b6\u5f00\u53d1\u5de5\u5177\u5c06\u4f1a\u662f\u5173\u952e\u3002<\/p>\n<p>Yii\u6846\u67b6\uff0c\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u3001\u9ad8\u6548\u7387\u3001\u9ad8\u6269\u5c55\u6027\u7684PHP\u6846\u67b6\uff0c\u88ab\u4f17\u591a\u5f00\u53d1\u4eba\u5458\u6240\u559c\u7231\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Yii\u6846\u67b6\u642d\u5efa\u4e00\u5957\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5Yii\u6846\u67b6<\/p>\n<p>Yii\u6846\u67b6\u7684\u5b89\u88c5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\uff0c\u89e3\u538b\u4e4b\u540e\u653e\u5230\u670d\u52a1\u5668\u4e0a\u5373\u53ef\u3002\u540c\u65f6\u8fd8\u9700\u8981\u5b89\u88c5Apache\u6216Nginx\u7b49Web\u670d\u52a1\u5668\u4ee5\u53caPHP\u73af\u5883\u3002<\/p>\n<p>\u4e8c\u3001\u914d\u7f6e\u6570\u636e\u5e93<\/p>\n<p>\u5728config\u76ee\u5f55\u4e0b\u7684main.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>'db'=&amp;gt;array(\n    'connectionString' =&amp;gt; 'mysql:host=localhost;dbname=mydatabase',\n    'emulatePrepare' =&amp;gt; true,\n    'username' =&amp;gt; 'root',\n    'password' =&amp;gt; 'mypassword',\n    'charset' =&amp;gt; 'utf8',\n),<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cconnectionString\u4e2d\u7684localhost\u53ef\u4ee5\u66ff\u6362\u4e3a\u6570\u636e\u5e93\u7684IP\u5730\u5740\uff0cdbname\u5219\u662f\u6570\u636e\u5e93\u540d\u79f0\u3002<\/p>\n<p>\u4e09\u3001\u521b\u5efa\u7cfb\u7edf\u6a21\u5757<\/p>\n<p>\u4f7f\u7528Yii\u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\uff0c\u9700\u8981\u6309\u7167\u529f\u80fd\u5c06\u6574\u4e2a\u5e94\u7528\u5206\u89e3\u6210\u4e00\u4e2a\u4e2a\u6a21\u5757\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7cfb\u7edf\u6a21\u5757\uff0c\u6765\u5904\u7406\u7528\u6237\u7684\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u7cfb\u7edf\u6a21\u5757<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u5728\u6a21\u5757\u4e2d\u521b\u5efa\u5bf9\u5e94\u7684\u76ee\u5f55\uff0c\u4f8b\u5982\u5728modules\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u53ebsystem\u7684\u76ee\u5f55\u3002\u5728system\u76ee\u5f55\u4e0b\uff0c\u65b0\u5efa\u4e00\u4e2a\u53ebSystemModule.php\u7684\u6587\u4ef6\uff0c\u7528\u6765\u5b9a\u4e49\u7cfb\u7edf\u6a21\u5757\u7684\u57fa\u672c\u4fe1\u606f\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class SystemModule extends CWebModule\n{\n    public $defaultController = 'User';\n    \/\/ \u5728\u7cfb\u7edf\u6a21\u5757\u4e2d\u6ce8\u518c\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u7ec4\u4ef6\n    public function init()\n    {\n        Yii::app()-&amp;gt;setComponents(array(\n            'user' =&amp;gt; array(\n                'class' =&amp;gt; 'CWebUser',\n                'stateKeyPrefix' =&amp;gt; 'system',\n                'autoRenewCookie' =&amp;gt; true,\n                'loginUrl' =&amp;gt; array('\/system\/user\/login'),\n            ),\n        ));\n        $this-&amp;gt;setImport(array(\n            'system.models.*',\n            'system.components.*',\n        ));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u7528\u6237\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u5728system\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u53ebUserController.php\u7684\u6587\u4ef6\uff0c\u7528\u6765\u8d1f\u8d23\u7528\u6237 CRUD \u64cd\u4f5c\u53ca\u767b\u5f55\u7b49\u529f\u80fd\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class UserController extends Controller\n{\n    public function actionLogin()\n    {\n        \/\/ \u7528\u6237\u767b\u5f55\u903b\u8f91\n    }\n    public function actionLogout()\n    {\n        \/\/ \u7528\u6237\u6ce8\u9500\u903b\u8f91\n    }\n    public function actionCreate()\n    {\n        \/\/ \u521b\u5efa\u65b0\u7528\u6237\u903b\u8f91\n    }\n    public function actionUpdate()\n    {\n        \/\/ \u66f4\u65b0\u7528\u6237\u4fe1\u606f\u903b\u8f91\n    }\n    public function actionDelete()\n    {\n        \/\/ \u5220\u9664\u7528\u6237\u903b\u8f91\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u5f00\u53d1\u8bfe\u7a0b\u6a21\u5757<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5f00\u53d1\u8bfe\u7a0b\u6a21\u5757\uff0c\u7528\u6765\u7ba1\u7406\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\u6240\u6709\u7684\u8bfe\u7a0b\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u8bfe\u7a0b\u6a21\u5757<\/li>\n<\/ol>\n<p>\u5728modules\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u53ebcourse\u7684\u76ee\u5f55\uff0c\u5728course\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u53ebCourseModule.php\u7684\u6587\u4ef6\uff0c\u7528\u6765\u5b9a\u4e49\u8bfe\u7a0b\u6a21\u5757\u7684\u57fa\u672c\u4fe1\u606f\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class CourseModule extends CWebModule\n{\n    public function init()\n    {\n        \/\/ \u6ce8\u518c\u7ec4\u4ef6\u5e76\u81ea\u52a8\u5bfc\u5165\u6a21\u5757\u4e2d\u7684\u7ec4\u4ef6\u7c7b\n        $this-&amp;gt;setImport(array(\n            'course.models.*',\n            'course.components.*',\n        ));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u8bfe\u7a0b\u4fe1\u606f\u6a21\u578b<\/li>\n<\/ol>\n<p>\u5728course\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u53ebCourse.php\u7684\u6587\u4ef6\uff0c\u7528\u6765\u5b9a\u4e49\u8bfe\u7a0b\u4fe1\u606f\u6a21\u578b\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class Course extends CActiveRecord\n{\n    public static function model($className=__CLASS__)\n    {\n        return parent::model($className);\n    }\n    public function tableName()\n    {\n        return 'course';\n    }\n    public function rules()\n    {\n        return array(\n            array('name', 'required'),\n            array('name', 'length', 'max'=&amp;gt;128),\n        );\n    }\n    public function attributeLabels()\n    {\n        return array(\n            'id' =&amp;gt; '\u8bfe\u7a0bID',\n            'name' =&amp;gt; '\u8bfe\u7a0b\u540d\u79f0',\n            'description' =&amp;gt; '\u8bfe\u7a0b\u4ecb\u7ecd',\n            'created_at' =&amp;gt; '\u521b\u5efa\u65f6\u95f4',\n            'updated_at' =&amp;gt; '\u66f4\u65b0\u65f6\u95f4',\n        );\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u8bfe\u7a0b\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u5728course\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u53ebCourseController.php\u7684\u6587\u4ef6\uff0c\u7528\u6765\u5904\u7406\u8bfe\u7a0b\u4fe1\u606f\u7684 CRUD \u64cd\u4f5c\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>class CourseController extends Controller\n{\n    public function actionIndex()\n    {\n        \/\/ \u663e\u793a\u6240\u6709\u8bfe\u7a0b\n    }\n    public function actionCreate()\n    {\n        \/\/ \u521b\u5efa\u65b0\u8bfe\u7a0b\n    }\n    public function actionUpdate()\n    {\n        \/\/ \u66f4\u65b0\u8bfe\u7a0b\u4fe1\u606f\n    }\n    public function actionDelete()\n    {\n        \/\/ \u5220\u9664\u8bfe\u7a0b\n    }\n    public function actionView()\n    {\n        \/\/ \u67e5\u770b\u5355\u4e2a\u8bfe\u7a0b\u4fe1\u606f\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e94\u3001\u89c6\u56fe\u5c42\u5f00\u53d1<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528Yii\u6846\u67b6\u7684\u89c6\u56fe\u5c42\u6280\u672f\uff0c\u6765\u5b9e\u73b0\u7f51\u7ad9\u7684\u524d\u7aef\u5c55\u793a\u3002\u5728\u89c6\u56fe\u5c42\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528Yii\u6846\u67b6\u63d0\u4f9b\u7684CActiveForm\u548cCHtml\u7b49\u7ec4\u4ef6\u7c7b\uff0c\u6765\u5feb\u901f\u521b\u5efa\u8868\u5355\u548cHTML\u5143\u7d20\u3002<\/p>\n<p>\u516d\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u4e86\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Yii\u6846\u67b6\u521b\u5efa\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\uff0c\u4e3b\u8981\u6d89\u53ca\u5230\u5b89\u88c5Yii\u6846\u67b6\u3001\u914d\u7f6e\u6570\u636e\u5e93\u3001\u521b\u5efa\u7cfb\u7edf\u6a21\u5757\u3001\u5f00\u53d1\u8bfe\u7a0b\u6a21\u5757\u4ee5\u53ca\u89c6\u56fe\u5c42\u5f00\u53d1\u7b49\u65b9\u9762\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u591f\u5bf9\u5f00\u53d1\u4eba\u5458\u4eec\u6709\u6240\u5e2e\u52a9\uff0c\u4e5f\u5e0c\u671b\u80fd\u591f\u5438\u5f15\u66f4\u591a\u7684\u6559\u80b2\u884c\u4e1a\u4ece\u4e1a\u8005\u6765\u8fdb\u5165\u5728\u7ebf\u6559\u80b2\u9886\u57df\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Yii\u6846\u67b6\u521b\u5efa\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\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>\u968f\u7740\u4e92\u8054\u7f51\u6280\u672f\u7684\u666e\u53ca\u548c\u4e92\u8054\u7f51\u7528\u6237\u7684\u589e\u52a0\uff0c\u6559\u80b2\u884c\u4e1a\u4e5f\u4e0d\u65ad\u5730\u5411\u7ebf\u4e0a\u8f6c\u79fb\uff0c\u6253\u9020\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\u5df2\u7ecf\u6210\u4e3a\u4e86\u73b0\u4ee3\u6559\u80b2\u884c\u4e1a\u7684\u8d8b\u52bf\u3002\u4e3a\u4e86\u5e94\u5bf9\u8fd9\u79cd\u8d8b\u52bf\uff0c\u9009\u62e9\u4e00\u4e2a\u9ad8\u6548\u7684\u6846\u67b6\u5f00\u53d1\u5de5\u5177\u5c06\u4f1a\u662f\u5173\u952e\u3002 Yii\u6846\u67b6\uff0c\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u3001\u9ad8\u6548\u7387\u3001\u9ad8\u6269\u5c55\u6027\u7684PHP\u6846\u67b6\uff0c\u88ab\u4f17\u591a\u5f00\u53d1\u4eba\u5458\u6240\u559c\u7231\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Yii\u6846\u67b6\u642d\u5efa\u4e00\u5957\u5728\u7ebf\u6559\u80b2\u7f51\u7ad9\u3002 \u4e00\u3001\u5b89\u88c5Yii\u6846\u67b6 Yii\u6846\u67b6\u7684\u5b89\u88c5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u5b98\u7f51\u4e0b\u8f7d\u5b89\u88c5\u5305\uff0c\u89e3\u538b\u4e4b\u540e\u653e\u5230\u670d\u52a1\u5668\u4e0a\u5373\u53ef\u3002\u540c\u65f6\u8fd8\u9700\u8981\u5b89\u88c5Apache\u6216Nginx\u7b49Web\u670d\u52a1\u5668\u4ee5\u53caPHP\u73af\u5883\u3002 \u4e8c\u3001\u914d\u7f6e\u6570\u636e\u5e93 \u5728config\u76ee\u5f55\u4e0b\u7684main.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u53c2\u6570\u3002\u5982\u4e0b\u6240\u793a\uff1a &#8216;db&#8217;=&amp;gt;array( &#8216;connectionString&#8217; =&amp;gt; &#8216;mysql:host=localhost;dbname=mydatabase&#8217;, &#8217;emulatePrepare&#8217; =&amp;gt; true, &#8216;username&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; &#8216;mypassword&#8217;, &#8216;charset&#8217; =&amp;gt; &#8216;utf8&#8217;, ), \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cconnectionString\u4e2d\u7684localhost\u53ef\u4ee5\u66ff\u6362\u4e3a\u6570\u636e\u5e93\u7684IP\u5730\u5740\uff0cdbname\u5219\u662f\u6570\u636e\u5e93\u540d\u79f0\u3002 \u4e09\u3001\u521b\u5efa\u7cfb\u7edf\u6a21\u5757 \u4f7f\u7528Yii\u6846\u67b6\u5f00\u53d1\u7f51\u7ad9\uff0c\u9700\u8981\u6309\u7167\u529f\u80fd\u5c06\u6574\u4e2a\u5e94\u7528\u5206\u89e3\u6210\u4e00\u4e2a\u4e2a\u6a21\u5757\u3002\u8fd9\u91cc\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u7cfb\u7edf\u6a21\u5757\uff0c\u6765\u5904\u7406\u7528\u6237\u7684\u57fa\u672c\u529f\u80fd\u3002 \u521b\u5efa\u7cfb\u7edf\u6a21\u5757 \u9996\u5148\uff0c\u5728\u6a21\u5757\u4e2d\u521b\u5efa\u5bf9\u5e94\u7684\u76ee\u5f55\uff0c\u4f8b\u5982\u5728modules\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u53ebsystem\u7684\u76ee\u5f55\u3002\u5728system\u76ee\u5f55\u4e0b\uff0c\u65b0\u5efa\u4e00\u4e2a\u53ebSystemModule.php\u7684\u6587\u4ef6\uff0c\u7528\u6765\u5b9a\u4e49\u7cfb\u7edf\u6a21\u5757\u7684\u57fa\u672c\u4fe1\u606f\u3002\u4ee3\u7801\u5982\u4e0b\uff1a class SystemModule extends CWebModule { public $defaultController = &#8216;User&#8217;; \/\/ \u5728\u7cfb\u7edf\u6a21\u5757\u4e2d\u6ce8\u518c\u7528\u6237\u8eab\u4efd\u9a8c\u8bc1\u7ec4\u4ef6 public function init() { Yii::app()-&amp;gt;setComponents(array( &#8216;user&#8217; =&amp;gt; array( &#8216;class&#8217; =&amp;gt; &#8216;CWebUser&#8217;, &#8216;stateKeyPrefix&#8217; =&amp;gt; &#8216;system&#8217;, &#8216;autoRenewCookie&#8217; [&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-23865","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23865","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=23865"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23865\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}