Building RESTful Web Service using Cygnite PHP Framework, MySQL


Hi guys, my last article I wrote about “Getting started with MySql event scheduler” .Now days most of the applications building in mind that we can also access data using cross device like mobile, web application. Shortly Web services are talking over the application world. So far I have developed many web service api which is interacting with mobile, tablet device to access data from the server. Today let me explain how to build a better REST api using Cygnite PHP Framework.

REST - Representational State Transfer

Building REST web service is very easy and also you can use the power of mvc architecture of Cygnite Framework to build powerful RESTApi services.

Installing Cygnite PHP Framework:

Install Composer into your system. Composer installation is the recommended way to install Cygnite PHP Framework or you may manually download it from git hub repo . Cygnite has beautiful documentation will lead you to quick start.

composer create-project cygnite/cygnite-application cygnite --prefer-dist

Make sure you have composer install in your system.

Configuring Database:

Let us configure the database connections before stepping into web service.



use Cygnite\Database;

    function ($config) {
             'db'  => 'mysql://root:webservice123@localhost/webservice_tutorial?charset=utf8'

We are done with database settings; let us move to routing to response to web service request.

Routing- RESTful Service:

Cygnite has powerful routing to respond to your REST service calls. Even you can make use of full featured MVC to respond to your request. In this article I will show you how to respond to your HTTP get method calls and return users as json array.

Step1: Responding to post request and calling controller:

Open apps/routes.php and paste below code to get the list of users from the database.

    function () {
        $data = file_get_contents("php://input"); 
        echo Router::call('Webservice.index', array("request" => $data));// call Webservice controller index method

Step2: Controller and Getting User Details using Model:

Create a controller called WebserviceController.php into apps/controllers directory and paste below code into your index method. It should be look like below.

namespace Apps\Controllers;

use Cygnite\Foundation\Application;
use Cygnite\Mvc\Controller\AbstractBaseController;
use Apps\Models\User;

class WebserviceController extends AbstractBaseController

    public function __construct()

    * Default method for your controller. We will return Users as json format
    * @access public
    public function indexAction($request = array())
       $request  = json_decode($request);
       if (isset($request['request']->USER_STATUS)) {   

       $users = User::findByStatus($request['request']->USER_STATUS);  
       $users = (array) $users;

         if (!empty($users)) {

             foreach ($users as $key => $value) {
                $data[] = $value->user_name;

            $response = array("data" => $data);

         } else {
            $response = array("error" => "There are no active users");

       } else {
              $response = array("error" => "Invalid request format");
       return json_encode($response);
}//End of the controller

Step3: Creating model to get Users:

I have a table called user into the webservice_tutorial database. So let us create the model called User and paste below code. Remember your table name model name should be same. For more info you may have a look at the documentation.

namespace Apps\Models;

use Cygnite\Database\ActiveRecord;

class User extends ActiveRecord
    //your database connection name
    protected $database = ‘webservice_tutorial’;
    protected $primaryKey = 'id';

    public function __construct()


That’s all. We are done. Just go to your rest client in and type the post url to get the result. You can see screenshot below we got the list of users from the database.


Similar way you can respond to POST, PUT, GET, DELETE, etc. Hope this article useful. If so please don’t forget to give a like or share with friends, also leave your comments below. Have a nice day. Keep visiting for upcoming posts.

No comments:

Post a Comment

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 All rights reserved. Powered By- Sanjoy Dey Productions