{"id":162,"date":"2015-05-22T13:55:28","date_gmt":"2015-05-22T08:55:28","guid":{"rendered":"http:\/\/developer.uz\/blog\/?p=162"},"modified":"2015-05-22T17:23:59","modified_gmt":"2015-05-22T12:23:59","slug":"restful-api-in-yii2","status":"publish","type":"post","link":"https:\/\/developer.uz\/blog\/restful-api-in-yii2\/","title":{"rendered":"\u041c\u0430\u0433\u0438\u044f RESTful API \u0432 Yii2"},"content":{"rendered":"<h2>\u041a\u043e\u043c\u0443 \u0438 \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u043e REST API<\/h2>\n<p>\u0412 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u043c\u0438\u0440\u0435, \u043c\u0438\u0440\u0435 \u0433\u0434\u0435 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0443 \u0438\u043c\u0435\u044e\u0442 \u0441\u043e\u0442\u043d\u0438 \u0432\u0438\u0434\u043e\u0432 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432, \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0448\u043b\u0438 \u0434\u0430\u043b\u0435\u043a\u043e \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0445 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u0432. \u0418 \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0438\u043c\u0435\u044e\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u044b, \u0432\u0441\u0435 \u0447\u0430\u0449\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0435 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u043e\u043a \u0442\u0438\u043f\u043e\u0432 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439. \u041d\u043e \u0441\u0443\u0442\u044c \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f. REST API &#8212; \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0435 \u0431\u0435\u043a\u0435\u043d\u0434-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043a\u043b\u0438\u0435\u043d\u0442\u0430. REST &#8212; \u044d\u0442\u043e \u043a\u043b\u0438\u0435\u043d\u0442-\u0441\u0435\u0440\u0432\u0435\u0440\u043d\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u0430\u044f \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0432\u044b\u0442\u0435\u043a\u0430\u044e\u0449\u0438\u043c\u0438 \u043e\u0442\u0441\u044e\u0434\u0430 \u043f\u043b\u044e\u0441\u0430\u043c\u0438. \u0410 \u043a\u0430\u043a \u0436\u0435 web? \u0412\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 &#8212; \u044d\u0442\u043e \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043b\u0438\u0435\u043d\u0442. \u0410 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u0435\u043c\u0438\u043c\u0438\u043b\u044c\u043d\u044b\u043c\u0438 \u0448\u0430\u0433\u0430\u043c\u0438 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u043e\u0441\u0442\u044c JS-MV* \u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u043e\u0432 (Angular, Backbone, CanJS, Ember \u0438 \u0442\u043f) \u0432\u0435\u0431 \u0441\u043a\u043e\u0440\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u0442 \u043d\u0430 \u043e\u0434\u043d\u043e-\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u043c \u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0445 HTTP-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<h2>RESTful API \u0432 Yii2 &#8212; \u043f\u0440\u043e\u0449\u0435 \u043d\u0435 \u0431\u044b\u0432\u0430\u0435\u0442<\/h2>\n<p>Yii2 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0435 RESTful API \u0437\u0430 \u043f\u0430\u0440\u0443 \u043c\u0438\u043d\u0443\u0442. \u041f\u0440\u0438\u0447\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0435 API \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0436\u0435 \u0444\u0440\u043e\u043d\u0442\u0435\u043d\u0449\u0438\u043a, \u043c\u0430\u043b\u043e \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u0441 yii \u0438 php \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 (\u0435\u0441\u043b\u0438 REST API \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438, \u0430 \u043b\u043e\u0433\u0438\u043a\u0430 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u0432 JS-\u0444\u0440\u0435\u0438\u043c\u0432\u043e\u0440\u043a\u0435).<\/p>\n<ul>\n<li>\u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u0438 \u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u0431\u0430\u0437\u043e\u0432\u043e\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 yii (\u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u043a\u0430\u043a, \u0447\u0438\u0442\u0430\u0435\u043c <a href=\"https:\/\/github.com\/yiisoft\/yii2-app-basic\" target=\"_blank\">\u0442\u0443\u0442<\/a>)\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0432 \u0411\u0414 \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e gii\n<li>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043b\u0430\u0441\u0441 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430 \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u0435\u0433\u043e \u043e\u0442 yii\\rest\\ActiveController\n<li>\u041f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043c\u043e\u0434\u0435\u043b\u044c<br \/>\n\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430:<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\nnamespace app\\controllers;\r\n\r\nuse yii\\rest\\ActiveController;\r\n\r\nclass UserController extends ActiveController\r\n{\r\n    public $modelClass = 'app\\models\\User';\r\n}\r\n<\/pre>\n<li>\u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u0440\u043e\u0443\u0442\u0438\u043d\u0433\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n'urlManager' =&gt; [\r\n            'enablePrettyUrl' =&gt; true,\r\n            'enableStrictParsing' =&gt; true,\r\n            'showScriptName' =&gt; false,\r\n            'rules' =&gt; [\r\n                ['class' =&gt; 'yii\\rest\\UrlRule', 'controller' =&gt; 'user'],\r\n            ],\r\n        ],\r\n        'request' =&gt; [\r\n            'parsers' =&gt; [\r\n                'application\/json' =&gt; 'yii\\web\\JsonParser',\r\n            ]\r\n        ],\r\n<\/pre>\n<\/ul>\n<p>RESTful API \u0433\u043e\u0442\u043e\u0432 \ud83d\ude42 \u0411\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.<\/p>\n<h2>\u0427\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c?<\/h2>\n<p>\u0423 \u0432\u0430\u0441 \u0442\u0435\u043f\u0435\u0440\u044c \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 url \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a API (\u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u0447\u0438\u0442\u0430\u0442\u044c).<\/p>\n<ul>\n<li>GET-\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\/users<\/strong> &#8212; \u043f\u043e\u0441\u0442\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0434\u0435\u043b\u0438<br \/>\n\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0438\u043c\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u0435\u0440\u0435\u0432\u0435\u0434\u0435\u043d\u043e \u0432\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e (\u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u043a\u043b\u0430\u0441\u0441\u0430 yii\\rest\\UrlRule \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0432 pluralize \u0432 false).<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\nnew ActiveDataProvider([\r\n            'query' =&gt; $modelClass::find(),\r\n        ]);\r\n<\/pre>\n<li>POST-\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\/users<\/strong> &#8212; \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438.\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n$model = new $this-&gt;modelClass([\r\n            'scenario' =&gt; $this-&gt;scenario,\r\n        ]);\r\n\r\n        $model-&gt;load(Yii::$app-&gt;getRequest()-&gt;getBodyParams(), '');\r\nif ($model-&gt;save()) {\r\n            $response = Yii::$app-&gt;getResponse();\r\n            $response-&gt;setStatusCode(201);\r\n            $id = implode(',', array_values($model-&gt;getPrimaryKey(true)));\r\n            $response-&gt;getHeaders()-&gt;set('Location', Url::toRoute([$this-&gt;viewAction, 'id' =&gt; $id], true));\r\n        } elseif (!$model-&gt;hasErrors()) {\r\n            throw new ServerErrorHttpException('Failed to create the object for unknown reason.');\r\n        }\r\n\r\n        return $model;\r\n<\/pre>\n<li>GET-\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\/users\/1<\/strong> &#8212; \u0432\u044b\u0431\u043e\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f 1 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043c\u043e\u0434\u0435\u043b\u0438.\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n $model = $modelClass::findOne($id);\r\n<\/pre>\n<li>PUT-\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\/users\/1<\/strong> &#8212; \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f 1 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u043c\u043e\u0434\u0435\u043b\u0438. \u041d\u0430\u0445\u043e\u0434\u0438\u0442 \u043c\u043e\u0434\u0435\u043b\u044c, \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u044c \u0432 \u0441\u043b\u0443\u0447\u0430\u0438 \u0443\u0441\u043f\u0435\u0445\u0430\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n$model = $modelClass::findOne($id);\r\n$model-&gt;load(Yii::$app-&gt;getRequest()-&gt;getBodyParams(), '');\r\nif ($model-&gt;save() === false &amp;&amp; !$model-&gt;hasErrors()) {\r\n            throw new ServerErrorHttpException('Failed to update the object for unknown reason.');\r\n        }\r\n\r\n        return $model;\r\n<\/pre>\n<li>DELETE-\u0437\u0430\u043f\u0440\u043e\u0441 <strong>\/users\/1<\/strong> &#8212; \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n $model = $this-&gt;findModel($id);\r\n if ($model-&gt;delete() === false) {\r\n            throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');\r\n        }\r\n\r\n        Yii::$app-&gt;getResponse()-&gt;setStatusCode(204);\r\n<\/pre>\n<\/ul>\n<h2>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043c\u0430\u0433\u0438\u044f RESTfulAPI \u0432 Yii2<\/h2>\n<p><strong>\u041e\u0442\u043a\u0443\u0434\u0430 \u0441\u0442\u043e\u043b\u044c\u043a\u043e action, \u043c\u044b \u0435\u0449\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438?<\/strong><br \/>\n\u0412 ActiveController \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u0438 \u043d\u0430\u0448 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0432\u0441\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 actions. \u0412\u043e\u0442 \u0442\u0430\u043a, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 Yii \u0437\u0430 \u043d\u0430\u0441 \u0443\u0436\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 actionIndex <\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n 'index' =&gt; [\r\n                'class' =&gt; 'yii\\rest\\IndexAction',\r\n                'modelClass' =&gt; $this-&gt;modelClass,\r\n                'checkAccess' =&gt; [$this, 'checkAccess'],\r\n            ],\r\n<\/pre>\n<p><strong>\u041a\u0430\u043a \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e REST \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u044b \u043e\u0442\u0434\u0430\u044e\u0442 \u0442\u043e ActiveRecord \u043c\u043e\u0434\u0435\u043b\u044c, \u0442\u043e \u0438 \u0432\u043e\u0432\u0441\u0435 ActiveDataProvider, \u0430 \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043e\u0442\u0434\u0430\u0435\u0442 xml \u0438\u043b\u0438 json-\u043c\u0430\u0441\u0441\u0438\u0432?<\/strong><br \/>\nActiveController \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d \u043e\u0442 yii\\rest\\Controller, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u043e\u043f\u0438\u0441\u0430\u043d \u043c\u0435\u0442\u043e\u0434 afterAction. \u041c\u0435\u0442\u043e\u0434 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043b\u0430\u0441\u0441\u0430 yii\\rest\\Serializer<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\npublic function serialize($data)\r\n    {\r\n        if ($data instanceof Model &amp;&amp; $data-&gt;hasErrors()) {\r\n            return $this-&gt;serializeModelErrors($data);\r\n        } elseif ($data instanceof Arrayable) {\r\n            return $this-&gt;serializeModel($data);\r\n        } elseif ($data instanceof DataProviderInterface) {\r\n            return $this-&gt;serializeDataProvider($data);\r\n        } else {\r\n            return $data;\r\n        }\r\n    }\r\n<\/pre>\n<p>\u0422.\u0435 \u0432 \u0441\u0432\u043e\u0435\u043c REST \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0435 \u043e\u0442\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043e\u0442 yii\\rest\\ActiveController \u0438\u043b\u0438 yii\\rest\\Controller \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u0434\u0443\u043c\u0430\u0442\u044c \u043e\u0431 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u043e\u0436\u043d\u043e \u0441\u043c\u0435\u043b\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c ActiveRecord \u043c\u043e\u0434\u0435\u043b\u044c, \u043b\u044e\u0431\u043e\u0439 \u043a\u043b\u0430\u0441\u0441, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 Arrayable \u0438\u043b\u0438 DataProviderInterface \u0438\u043b\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432.<br \/>\n\u0417\u0430 \u0444\u043e\u0440\u043c\u0430\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 (\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 JSON \u0438\u043b\u0438 XML) \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 yii\\filters\\ContentNegotiator \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u043e\u0442\u0432\u0435\u0442\u0430 \u0431\u044b\u043b \u0437\u0430\u043f\u0440\u043e\u0448\u0435\u043d, \u0442.\u0435 \u0432\u0430\u043c, \u043a\u0430\u043a \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443 \u043e\u0431 \u044d\u0442\u043e\u043c \u0434\u0443\u043c\u0430\u0442\u044c \u0442\u043e\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e. <\/p>\n<h2>\u041d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0442\u044e\u043d\u0438\u043d\u0433 REST API \u0432 Yii2<\/h2>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0432\u0432\u043e\u0434\u043d\u0430\u044f, \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043b\u0438\u0448\u044c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u044b\u0445 &#171;\u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a&#187; \u0434\u043b\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0437\u0430 \u043f\u0430\u0440\u0443 \u043c\u0438\u043d\u0443\u0442 RESTful API:<br \/>\n<strong>\u041a\u0430\u043a \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u0447\u0435\u0440\u0435\u0437 REST API? <\/strong><br \/>\n\u0412 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 yii\\rest\\UrlRule \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0430\u0448\u0435 API \u0447\u0435\u0440\u0435\u0437 only \u0438\u043b\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u0447\u0435\u0440\u0435\u0437 except<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\n'urlManager' =&gt; [\r\n            'enablePrettyUrl' =&gt; true,\r\n            'enableStrictParsing' =&gt; true,\r\n            'showScriptName' =&gt; false,\r\n            'rules' =&gt; [\r\n                ['class' =&gt; 'yii\\rest\\UrlRule', \r\n                 'controller' =&gt; 'user'\r\n                 'except' =&gt; ['delete'],\r\n                ],\r\n            ],\r\n        ],\r\n<\/pre>\n<p><strong>\u041a\u0430\u043a \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u043f\u043e\u043b\u044f\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u0447\u0435\u0440\u0435\u0437 REST API?<\/strong><br \/>\n\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 \u043c\u043e\u0434\u0435\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b fields() \u0438 extraFields()<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\npublic function fields()\r\n    {\r\n        return ['id', 'email', 'username'];\r\n    }\r\n public function extraFields()\r\n    {\r\n        return ['status'];\r\n    }\r\n<\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 GET \/users \u0431\u0443\u0434\u0443\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u044f id, email \u0438 username. \u0410 \u043f\u0440\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 GET \/users?expand=status \u0432\u0435\u0440\u043d\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 id, email, username \u0438 status.<br \/>\n<strong>\u041a\u0430\u043a \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u043e\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043b\u044f REST API?<\/strong><br \/>\n\u0412\u0441\u0435 \u0442\u0430\u043a-\u0436\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u0438\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 fields() \u0432 \u0432\u0438\u0434\u0435 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/p>\n<pre class=\"brush: php; title: \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434; notranslate\" title=\"\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434\">\r\npublic function fields()\r\n    {\r\n        return [\r\n'id', \r\n'email', \r\n'username' =&gt; function ($model) {\r\n            return $model-&gt;first_name . ' ' . $model-&gt;last_name;\r\n        }\r\n];\r\n    }\r\n public function extraFields()\r\n    {\r\n        return ['status'];\r\n    }\r\n<\/pre>\n<p>\n\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f REST API \u0432 Yii2 \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438. \u0417\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u043d\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e &#8212; \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043f\u0440\u0430\u0432, \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043c\u043e\u0434\u0435\u043b\u044c\u044e, \u043b\u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u043f\u043e\u0437\u0436\u0435 \u0443\u0434\u0430\u0441\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0442\u043e\u0440\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u0438 \u043f\u0440\u043e\u0431\u0435\u043b\u044b. \u041d\u0443 \u0430 \u043f\u043e\u043a\u0430, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441\u044b &#8212; \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0445, \u0430 \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041a\u043e\u043c\u0443 \u0438 \u0437\u0430\u0447\u0435\u043c \u043d\u0443\u0436\u043d\u043e REST API \u0412 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u043c\u0438\u0440\u0435, \u043c\u0438\u0440\u0435 \u0433\u0434\u0435 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0418\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0443 \u0438\u043c\u0435\u044e\u0442 \u0441\u043e\u0442\u043d\u0438 \u0432\u0438\u0434\u043e\u0432 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432, \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u0448\u043b\u0438 \u0434\u0430\u043b\u0435\u043a\u043e \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0445 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u043e\u0432. \u0418 \u043d\u0435 \u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0438\u043c\u0435\u044e\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u044b, \u0432\u0441\u0435 \u0447\u0430\u0449\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u043d\u044b\u0435 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0434\u043b\u044f [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[17,13],"_links":{"self":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/162"}],"collection":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/comments?post=162"}],"version-history":[{"count":8,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/posts\/162\/revisions\/170"}],"wp:attachment":[{"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/media?parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/categories?post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.uz\/blog\/wp-json\/wp\/v2\/tags?post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}