Yii-运行中的RESTful API

controller类是从yii \ rest \ ActiveController类扩展的,该类实现了常见的RESTful操作。我们指定$ modelClass属性,以便控制器知道要使用哪个模型来处理数据。

步骤1-在controllers文件夹内创建一个名为UserController.php的文件。

<?php
   namespace app\controllers;
   use yii\rest\ActiveController;
   class UserController extends ActiveController {
      public $modelClass = 'app\models\MyUser';
   }
?>

接下来,我们需要设置urlManager组件,以便可以使用有意义的HTTP动词和漂亮的URL访问和操作用户数据。为了让API访问JSON中的数据,我们应该配置请求应用程序组件的parsers属性。

第2步-以这种方式修改config / web.php文件-

<?php
   $params = require(__DIR__ . '/params.php');
   $config = [
      'id' => 'basic',
      'basePath' => dirname(__DIR__),
      'bootstrap' => ['log'],
      'components' => [
         'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is 
               //required by cookie validation
            'cookieValidationKey' => 'ymoaYrebZHa8gURuolioHGlK8fLXCKjO',
         ],
         'cache' => [
            'class' => 'yii\caching\FileCache',
         ],
         'user' => [
            'identityClass' => 'app\models\User',
            'enableAutoLogin' => true,
         ],
         'errorHandler' => [
            'errorAction' => 'site/error',
         ],
         'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            // send all mails to a file by default. You have to set
            // 'useFileTransport' to false and configure a transport
            // for the mailer to send real emails.
            'useFileTransport' => true,
         ],
         'log' => [
            'traceLevel' => YII_DEBUG ? 3 : 0,
            'targets' => [
               [
                  'class' => 'yii\log\FileTarget',
                  'levels' => ['error', 'warning'],
               ],
            ],
         ],
         'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => true,
            'showScriptName' => false,
            'rules' => [
               ['class' => 'yii\rest\UrlRule', 'controller' => 'user'],
            ],
         ],
         'request' => [
            'parsers' => [
               'application/json' => 'yii\web\JsonParser',
            ]
         ],
         'db' => require(__DIR__ . '/db.php'),
      ],
      'modules' => [
         'hello' => [
            'class' => 'app\modules\hello\Hello',
         ],
      ],
      'params' => $params,
   ];
   if (YII_ENV_DEV) {
      // configuration adjustments for 'dev' environment
      $config['bootstrap'][] = 'debug';
      $config['modules']['debug'] = [
         'class' => 'yii\debug\Module',
      ];
      $config['bootstrap'][] = 'gii';
      $config['modules']['gii'] = [
         'class' => 'yii\gii\Module',
      ];
   }
   return $config;
?>

只需花费最少的精力,我们就构建了一个RESTful API来访问用户数据。API包括-

  • GET / users-逐页列出所有用户
  • HEAD / users-显示用户列表的概述信息
  • POST / users-创建一个新用户
  • GET / users / 20-返回用户的详细信息20
  • HEAD / users / 20-显示用户20的概述信息
  • PATCH / users / 20和PUT / users / 20-更新用户20
  • DELETE / users / 20-删除用户20
  • 选项/用户-显示有关端点/用户的支持动词
  • 选项/ users / 20-显示有关端点/ users / 20的支持动词

注意,Yii自动使控制器名称复数。

步骤3-现在,打开邮递员,输入http:// localhost:8080 / users,然后单击“发送”。您将看到以下内容。

Yii-运行中的RESTful API

步骤4-要创建新用户,请将请求类型更改为POST,添加两个正文参数:名称和电子邮件,然后单击“发送”。

Yii-运行中的RESTful API

步骤5-您可以使用fields参数指定结果中应包括哪些字段。例如,URL http:// localhost:8080 / users?fields = id,name将仅返回idname字段,如以下屏幕截图所示。

Yii-运行中的RESTful API

作者:terry,如若转载,请注明出处:https://www.web176.com/yii/392.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年10月23日 下午3:41
下一篇 2020年10月23日 下午3:47

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注