PHPackages                             katanyoo/yii2-app-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. katanyoo/yii2-app-api

ActiveProject[Framework](/categories/framework)

katanyoo/yii2-app-api
=====================

Yii 2 API Project Template

3.1(8y ago)161BSD-3-ClauseHTMLPHP &gt;=5.4.0

Since Oct 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/katanyoo/yii2-app-api)[ Packagist](https://packagist.org/packages/katanyoo/yii2-app-api)[ Docs](http://github.com/katanyoo)[ RSS](/packages/katanyoo-yii2-app-api/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (12)Versions (18)Used By (0)

 [ ![](https://avatars0.githubusercontent.com/u/993323) ](https://github.com/katanyoo/yii2-app-api)

Yii 2 RESTful API Project Template
==================================

[](#yii-2-restful-api-project-template)

DIRECTORY STRUCTURE
-------------------

[](#directory-structure)

```
  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

```

REQUIREMENTS
------------

[](#requirements)

The minimum requirement by this project template that your Web server supports PHP 5.4.0.

INSTALLATION
------------

[](#installation)

### Install via Composer

[](#install-via-composer)

If you do not have [Composer](http://getcomposer.org/), you may install it by following the instructions at [getcomposer.org](http://getcomposer.org/doc/00-intro.md#installation-nix).

You can then install this project template using the following command:

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

```

Now you should be able to access the application through the following URL, assuming `api` is the directory directly under the Web root.

```
http://localhost/api/web/

```

### Install from an Archive File

[](#install-from-an-archive-file)

Extract the archive file downloaded from [github.com](http://github.com/katanyoo/yii2-app-api) to a directory named `api` that is directly under the Web root.

Set cookie validation key in `config/web.php` file to some random secret string:

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

You can then access the application through the following URL:

```
http://localhost/api/web/

```

CONFIGURATION
-------------

[](#configuration)

### API Versioning

[](#api-versioning)

Edit the file `config/modules.php` for each of API versions

### Database

[](#database)

Edit the file `config/db.php` with real data, for example:

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

**NOTES:**

- Yii won't create the database for you, this has to be done manually before you can access it.
- Check and edit the other files in the `config/` directory to customize your application as required.
- Refer to the README in the `tests` directory for information specific to api application tests.

Basic usage (HTTP Client)
-------------------------

[](#basic-usage-http-client)

Performing HTTP GET request with mime type detection:

```
// 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');
```

You can disable this behavior by specifying `$detectMimeType` option to whole component or single call

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

Make request with custom options:

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

Read more about this options in [Guzzle 6 documentation](http://guzzle.readthedocs.org/en/latest/request-options.html)

HTTP methods
------------

[](#http-methods)

You can make request with several ways:

1. Call shortcut method (`get()`, `post()`, `put()`, `delete()`, etc.)
2. Call `request()` method

All shortcut methods has the same signature except `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
);
```

> **NOTE**: you still can make a GET request with body via `request()` function

Asynchronous calls
------------------

[](#asynchronous-calls)

To make an asynchronous request simly add `Async` to end of request method:

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

> **NOTE**: mime type detection is not supported for asynchronous calls

Read more about asynchronous requests in [Guzzle 6 documentation](http://guzzle.readthedocs.org/en/latest/quickstart.html#async-requests)

Request body
------------

[](#request-body)

Types you can pass as a body of request:

1. **Arrayable object** (ActiveRecord, Model etc.) - will be encoded into JSON object
2. **Array** - will be sent as form request (x-form-urlencoded)

Any other data passed as body will be sent into Guzzle without any transformations.

Read more about request body in [Guzzle documentation](http://guzzle.readthedocs.org/en/latest/request-options.html#body)

API Document Generating
-----------------------

[](#api-document-generating)

```
./yii doc/gen

```

For Windows user

```
yii doc/win-gen

```

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Recently: every ~26 days

Total

17

Last Release

2957d ago

Major Versions

1.9.1 → 2.02018-02-15

2.0 → 3.02018-04-05

### Community

Maintainers

![](https://www.gravatar.com/avatar/3f0278359a82d7c56e2feac560cfa08ba68e2bdf217e43ba9560384c1803bf6e?d=identicon)[katanyoo](/maintainers/katanyoo)

---

Top Contributors

[![katanyoo](https://avatars.githubusercontent.com/u/1026942?v=4)](https://github.com/katanyoo "katanyoo (22 commits)")

---

Tags

apiframeworkserviceyii2project template

### Embed Badge

![Health badge](/badges/katanyoo-yii2-app-api/health.svg)

```
[![Health](https://phpackages.com/badges/katanyoo-yii2-app-api/health.svg)](https://phpackages.com/packages/katanyoo-yii2-app-api)
```

###  Alternatives

[izyue/yii2-app-advanced

Yii 2 Advanced Project Template

1281.9k](/packages/izyue-yii2-app-advanced)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
