Create a Cross-Platform REST API in Yii2

by Saquib Rizwan  May 6, 2017

When you are working on the cross-platform application, REST API’s become essential requirements of the process. This is why the Internet is full of third party API’s that provide a wide range of functionality and usability. Yii is a major PHP framework for rapid web development with a well-defined MVC architecture. In order to create a cross-platform Yii2 application, you need to create a REST API in Yii2.

In this article, I will develop a REST API in Yii 2 framework. The process covers:

  • Installation of Yii 2 framework on Cloudways
  • Database and table(s) creation
  • Database model using Gii
  • Controller creation using Gii
  • HTTP Calls (GET, PUT, POST, DELETE)

Introduction to REST API

REST stands for Representational State Transfer.  A REST API is a web service implemented using HTTP protocol and the principles of REST. It is a group of resources which uses HTTP methods likes GET, PUT, POST, DELETE.

Install Yii 2 Framework on Cloudways

First of all, sign up for a free account at Cloudways. Next, login to your account and create a new server. Fill in the server and the application detail and select PHP Stack as your application. Enter your application, server and project’s name.

Note: You can host unlimited applications on a single server.

Select your favorite cloud provider, scale your server size according to your needs, select your location and click the Launch button. In a matter of minutes, the cloud server and the application is ready for next steps. Check out the following GIF for more details:


Now that your server and application is ready, open your server by clicking the server name.

Click Launch SSH Terminal as shown below:

Login with the username and password provided in the Master Credentials.

Now that you are connected to your server, install Yii2 framework using composer.

Type the following commands in the given order to go to the newly created Yii2 folder and start the installation process:

The installation of the Yii 2 framework will begin:

Once the installation finishes, go to the application tab on Cloudways and select your application. Click on the button (as shown below) to access your application. Add /restapi/web/ to the URL and hit Enter.

You will see the main page:

Create Database and Table

Next, I will create a simple database with a table named Student. In order to create the database, go to the application and launch the database manager.

Type in the following command in the SQL command field:

The database is now ready. Next, add its credentials in the db.php file.  Go to your application in Cloudways to get the database credentials.

Next, go to config/db.php and add dbname, username and password as shown below:

Create Database Model Using Gii

Go to the application URL and add restapi/web/index.php?r=gii at the end. Click on to Model Generator as shown below:

Fill in the table name and model name as student, and generate the model.

Now that the model has been created, I will create the scenario in the model for validation. Go to models/Student.php file and type the following command in the first line of the Student Class.


After that add the following function:

Create Controller Using Gii

To create the controller, go to the restapi/web/index.php?r=gii again and click on the Controller Generator.

Create HTTP Calls

Now to manipulate data across different platforms, I will create the following API Actions.

Create Student Record

To create a Student record using the API, I will use the POST method.

Go to the controllers/StudentController.php file and create a new function called actionCreateStudent(). In this function, first set the application response format to JSON. After that, create a Student model object to create a record in the database. Then, in order to validate, I will use the scenario that I created in the Model.

Now if the validation is successful, I will add the record to the database through the save() function. This will save the model object in the database and return the status accordingly.

Now to test the Create Student action, i will use Postman extension. Go to the application URL and add /restapi/web/index.php?r=student/create-student  to the URL. Copy the complete link and paste it in the Postman as shown below:

Get All the Students Record

Now create another function in the Controller for getting the list of all the Student records in the JSON format.

This is how this will be done. Create a function actionGetStudent(). Set the response in JSON format. Then get all the data by using the find() function.

Now to test the GET Student action, go to your application URL and add /restapi/web/index.php?r=student/get-student  to it. Copy the complete link and paste it in the Postman as shown below:

Update Student Records

Now, create another function in the Controller to update student records.

The process involves: Create a function actionUpdateStudent(). Set the response in JSON format. Then get the attribute in the variable and get the student record for a particular id by using find()->where. Then, set the value of attributes in the Student object, and then update through save() function.

Now to test the Update Student action, go to your application URL and add /restapi/web/index.php?r=student/Update-student  to the URL. Copy the complete link and paste it in the Postman:

Delete Student Records

Create another function in the Controller for updating student records.

This function will create a function actionUpdateStudent() and set the response in JSON format. Then, get attribute in the variable and get the student record for the specific id by using find()->where. Next, delete student through delete() function.

 

Conclusion

In this rather long tutorial, I created a REST API in Yii 2. In addition, I also discussed four API actions that handle data manipulation on the HTTP request calls and returns the output in JSON format.

 

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Saquib Rizwan

Saquib is a PHP Community Expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is well versed in PHP and regularly contributes to open source projects. For fun, he enjoys gaming, movies and hanging out with friends. You can email him at saquib.rizwan@cloudways.com

Stay Connected:

You Might Also Like...