katanyoo/yii2-app-api

Yii 2 API项目模板

安装: 61

依赖: 0

建议: 0

安全: 0

星星: 1

关注者: 2

分支: 0

语言:HTML

类型:项目

3.1 2018-04-05 21:31 UTC

This package is not auto-updated.

Last update: 2024-09-21 18:34:19 UTC


README

Yii 2 RESTful API项目模板


目录结构

  assets/             contains assets definition
  commands/           contains console commands (controllers)
  config/             contains application configurations
  controllers/        contains Controller classes
  modules/            contains Module for api
    v1/               contains Controller classes and Model classes
      controllers/    contains Module controller classes
      models/         contains Model classes
  mail/               contains view files for e-mails
  runtime/            contains files generated during runtime
  tests/              contains various tests for the api application
  vendor/             contains dependent 3rd-party packages
  views/              contains view files for the Web application
  web/                contains the entry script and Web resources

需求

本项目模板最低要求是Web服务器支持PHP 5.4.0。

安装

通过Composer安装

如果您没有Composer,可以按照getcomposer.org上的说明进行安装。

然后,您可以使用以下命令安装此项目模板

php composer.phar create-project --prefer-dist katanyoo/yii2-app-api api

现在,您应该可以通过以下URL访问应用程序,假设api是Web根目录下的目录。

https:///api/web/

从存档文件安装

将从github.com下载的存档文件解压到Web根目录下的名为api的目录。

config/web.php文件中将cookie验证密钥设置为某个随机的密钥字符串

'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => '<secret random string goes here>',
],

然后您可以通过以下URL访问应用程序

https:///api/web/

配置

API版本控制

编辑config/modules.php文件以设置每个API版本

数据库

使用真实数据编辑config/db.php文件,例如

return [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=yii2api',
    'username' => 'root',
    'password' => '1234',
    'charset' => 'utf8',
];

注意

  • Yii不会为您创建数据库,您必须手动创建才能访问。
  • 检查并编辑config/目录中的其他文件以根据需要自定义应用程序。
  • 有关特定于API应用程序测试的信息,请参阅tests目录中的README。

基本用法(HTTP客户端)

执行具有MIME类型检测的HTTP GET请求

// Result is html text
$text = Yii::$app->httpclient->get('http://httpbin.org/html');

// Result is SimpleXMLElement containing parsed XML
$xml = Yii::$app->httpclient->get('http://httpbin.org/xml');

// Result is parsed JSON array
$json = Yii::$app->httpclient->get('http://httpbin.org/get');

您可以通过指定组件或单个调用的$detectMimeType选项来禁用此行为

// Result is Guzzle `Response` object
$text = Yii::$app->httpclient->get('http://httpbin.org/xml', [], false);

使用自定义选项进行请求

$text = Yii::$app->httpclient->get('http://httpbin.org/xml', [
    'proxy' => 'tcp://:8125'
]);

有关这些选项的更多信息,请参阅Guzzle 6文档

HTTP方法

您可以使用多种方式发出请求

  1. 调用快捷方法(get()post()put()delete()等)
  2. 调用request()方法

所有快捷方法除了get()都具有相同的签名

// Synchronous GET request
Yii::$app->httpclient->get(
    $url, // URL
    [], // Options
    true // Detect Mime Type?
);

// Synchronous POST (and others) request
Yii::$app->httpclient->post(
    $url, // URL
    $body, // Body
    [], // Options
    true // Detect Mime Type?
);

// Asynchronous GET request
Yii::$app->httpclient->getAsync(
    $url, // URL
    [] // Options
);

// Asynchronous POST (and others) request
Yii::$app->httpclient->postAsync(
    $url, // URL
    $body, // Body
    [] // Options
);

注意:您仍然可以通过request()函数发出带主体的GET请求

异步调用

要发出异步请求,只需将Async添加到请求方法末尾即可

// PromiseInterface
$promise = Yii::$app->httpclient->postAsync('http://httpbin.org/post');

注意:异步调用不支持MIME类型检测

有关异步请求的更多信息,请参阅Guzzle 6文档

请求主体

您可以将其作为请求主体的类型

  1. Arrayable对象(ActiveRecord、Model等)-将编码为JSON对象
  2. 数组 -将作为表单请求(x-form-urlencoded)发送

任何其他作为主体传递的数据将直接通过Guzzle发送,不做任何转换。

有关请求主体的更多信息,请参阅Guzzle文档

API文档生成

./yii doc/gen

适用于Windows用户

yii doc/win-gen