Skip to content

Commit

Permalink
added "Usage" section to README
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Chepurnoy committed Oct 14, 2017
1 parent c7a2255 commit a1de006
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,99 @@ class SiteController extends Controller
}
}
```

Usage
-------------
1) Creating a Controller

First, create a controller class `app\controllers\UserController` as follows:

```php
namespace app\controllers;

use yii\rest\ActiveController;

/**
* Class UserController
*/
class UserController extends Controller
{
/**
* @SWG\Get(path="/user",
* tags={"User"},
* summary="Retrieves the collection of User resources.",
* @SWG\Response(
* response = 200,
* description = "Category collection response",
* @SWG\Schema(ref = "#/definitions/User")
* ),
* )
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => User::find(),
]);

return $dataProvider;
}
}
```
The controller class extends from `yii\rest\ActiveController`, which implements a common set of RESTful actions.

2) Creating `User` definition

You need to create folder `definitions` and add `User` definition class as follows:

```php
<?php

namespace api\models\definitions;

/**
* @SWG\Definition(required={"username", "email"})
*
* @SWG\Property(property="id", type="integer")
* @SWG\Property(property="email", type="string")
* @SWG\Property(property="username", type="string")
*/
class User
{
}
```

3) Configuring URL Rules

Then, modify the configuration of the urlManager component in your application configuration:
```php
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
'GET,HEAD users' => 'user/index',
],
]
```

4) Enabling JSON Input

To let the API accept input data in JSON format, configure the parsers property of the request application component to use the `yii\web\JsonParser` for JSON input:
```php
'request' => [
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
]
```

Trying it Out
-------------

Now you can access to swagger documentation section through the following URL:

http://localhost/path/to/index.php?r=site/docs




0 comments on commit a1de006

Please sign in to comment.