Dominus77 / yii2-advanced-start

Yii2 Start Project Advanced Template
https://dominus77.github.io/yii2-advanced-start/
MIT License
23 stars 12 forks source link

About RESTful-API #17

Closed polinwei closed 6 years ago

polinwei commented 6 years ago

Hi Sir:

If I want to add a RESTful-API for blog , Which directory is better to put? like 'api\modules\v1\controllers'. Is there anything to pay attention to?

Dominus77 commented 6 years ago

Hello! You can use the analogy of User api. 'api\modules\v1\controllerlers\BlogController.php'. http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html

Dominus77 commented 6 years ago

You can do in by analogy the backend, as an example: https://github.com/Dominus77/yii2-advanced-start/tree/api_module_dev Api is made for the users module References for access:

http://mysite.com/api/users
http://mysite.com/api/user/1
Dominus77 commented 6 years ago

Basic moments:

  1. In Modules.php we add a variable $isApi https://github.com/Dominus77/yii2-advanced-start/blob/a6514a07d1cb95e18b87cc59394b0eb0e0d509af/modules/users/Module.php#L27
  2. Let's add switching of controllers https://github.com/Dominus77/yii2-advanced-start/blob/a6514a07d1cb95e18b87cc59394b0eb0e0d509af/modules/users/Module.php#L58-L60
  3. In the api config we connect the module as in the backend https://github.com/Dominus77/yii2-advanced-start/blob/a6514a07d1cb95e18b87cc59394b0eb0e0d509af/api/config/main.php#L25-L27
  4. In the same place we connect BootstrapApi.php https://github.com/Dominus77/yii2-advanced-start/blob/a6514a07d1cb95e18b87cc59394b0eb0e0d509af/api/config/main.php#L13-L16
  5. Add the api folder and Controller DefaultController.php to the Users controllers. https://github.com/Dominus77/yii2-advanced-start/blob/api_module_dev/modules/users/controllers/api/DefaultController.php
  6. Let's add a model for api https://github.com/Dominus77/yii2-advanced-start/blob/api_module_dev/modules/users/models/api/User.php
  7. Add the file BootstrapApi.php which was connected to api/config/main.php https://github.com/Dominus77/yii2-advanced-start/blob/api_module_dev/modules/users/BootstrapApi.php

Now for Users RESTful is available:

http://yii2-advanced-start.loc/api/users  // All users
http://yii2-advanced-start.loc/api/user/1 // User with ID=1

Since we have not disabled the old api in the config, it is also available:

http://yii2-advanced-start.loc/api/v1/users   // All users
http://yii2-advanced-start.loc/api/v1/users/1 // User with ID=1

By this principle, you can add api to other modules.

polinwei commented 6 years ago

Hi Sir: Thanks. It is helpful. I will try it.

polinwei commented 6 years ago

Why is the value 'controller' => [ 'users/default'] in BootstrapApi.php ?

polinwei commented 6 years ago

I think I have fully understand. I also finished the REST for blog. Thanks for your detail document.

Dominus77 commented 6 years ago

Why is the value 'controller' => [ 'users/default'] in BootstrapApi.php ?

http://www.yiiframework.com/doc-2.0/guide-rest-quick-start.html#configuring-url-rules