Build REST API With Yii2

yii provides a whole set of tools to simplify the task of implementing RESTful Web Service APIs. Here I am goint build an example REST APIs with yii2. Lets get started.

  1. open phpstorm, create an empty project, and initial composer.
  2. Open cmd, locate to project folder
  3. Run following two composer commends:

    composer global require “fxp/composer-asset-plugin:^1.2.0”
    composer create-project –prefer-dist yiisoft/yii2-app-basic basic

    If you need learn more about composer, checkout: https://getcomposer.org/

  4. Now upload whole project to the server

    App Store

  5. Change the ownership of the website folder

    cd /home/wwwroot/project1.abc.com
    chown www:www ./ -R

  6. Open the browser

    App Store

  7. Database connection

    App Store

  8. Configure the url manager

    App Store

    App Store

  9. Now we can generate model class by Gii. Open the url:
    http://project1.abc.com/basic/web/index.php?r=gii

    App Store

    We have customer table, just use this table

    App Store

    App Store

    App Store

  10. Use this model as the model of our controller

    App Store

  11. Make a request

    App Store

    App Store