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.

Getting Started With Mysql Event Scheduler


Hi guys, my last article I shown you “How to generate database oriented Form using Cygnite simple command”. Today I would like to write about MySql Event Scheduler. MySql Event Scheduler very useful concept if you are dealing with large scale application like email system, online shopping, or may be activity history management application. Where every day thousands of data getting stored into the database which create a heavy load into your database and slow down your application. You may no longer required existing activity history or want to archive data, this is where Mysql Event scheduler come to play. Event useful to create backup, delete unnecessary rows from database so on. Let me show you how to start with Mysql scheduler.

Creating a Table:

Let us create a sample table activity_history to store each and every activity of user in the application. You may compare with similar stuffs at facebook where each and every user activity displays at right top corner.

CREATE TABLE activity_history 
user_id INT(11),,
description INT(11),

Enabling Event Scheduler:

Before creating or scheduling event you need enable event by executing below command.

SET GLOBAL event_scheduler = ON;
SET GLOBAL event_scheduler = 1;

Similarly you can also turn off the event as below.

SET GLOBAL event_scheduler = OFF;

Confirming Event

After enabling you may confirm your event by


Creating Event:

CREATE EVENT delete_activity
DELETE FROM activity_history WHERE created_at <= DATE_SUB(NOW(), INTERVAL 15 DAY) ;

Above event will run every day and delete 15 days old activity history from the table.

Altering Event:

Suppose you want to alter the old event you can do as below.

ALTER EVENT delete_activity
    STARTS '2014-07-01 01:00:00'

If you would like to rename Event

ALTER EVENT delete_activity RENAME TO delete_activity_event;

Displaying Mysql Events:

Below command will display all the events available into your database.


Deleting Event:

You may delete unnecessary event by executing.

DROP EVENT delete_activity_event;


Hope this article useful. If so please don’t forget to give a small like or share with friends, also leave your comments below.

Have a nice day. Keep visiting for latest posts.

Generate Form Class Using Cygnite Form Generator Comand


Hi guys, my last article was about “Getting started with PHP dependency manager Composer”. Today I would like to introduce you with another best tool to generate forms, Cygnite Form generator. Writing html form is old fashion, now days most of the passionate developers does same thing object oriented way. Cygnite Framework CLI gives you breeze and make your development amusing. In some case you may don’t want to generate entire crud operation which Cygnite does cool way.

You are looking for Form generator?

Cygnite CLI is the great tool to cater your development time, generate you form based on your database table with single line of console command. Sounds interesting? Yes.

Let me show you how to get started with forms, before that you need to know folder structure, database configuration etc. with some simple steps.

Cygnite Framework: Folder Structure

Database Configuration

Open database.php file and provide your database configuration as below.


use Cygnite\Database;

    function ($config) {
             'db'  => 'mysql://username:password@hostname/database_name?charset=utf8'

This is one time configuration. You are ready to generate your awesome forms. Remember all your forms will be generating based on your table exists into your database.

Cygnite CLI: Running Form Generator Command

We are ready. Open your console window, Command prompt if you are using Windows and terminal if you are using Ubuntu or linux.

Changing directory. Go to your Cygnite root directory as below. I have installed Cygnite into cygnite-app.

cd /var/www/cygnite-app/console/bin/

Now let us generate form against registration table.

Run below command and press enter.

 php cygnite generate:form table_name database


 php cygnite generate:form registration// Default database connection

Finding and Rendering Form

You can find your form class inside your form component directory. You can see /cygnite-app/apps/components/form/RegistrationForm.php generated. You can generate and access your form as below.

use Apps\Components\From\RegistrationForm;

$form = new RegisterForm(); // create a form object

echo $form->buildForm()->render(); // render your registration form.

Isn’t simple? You may alter the form based on your need. You may have a look at the documentation if you need further detail information about Cygnite Form Builder to alter form elements.


Hope it is useful, once you are ready with database; you are ready to generate n number of forms. Cygnite Framework is under active development and your suggestions and feedback will help us to fulfill all your needs.

If you find above tutorial helpful, please don’t forget to give a small like or share with friends, also leave your comments below.

Have a nice day. Keep visiting for interesting articles.

Getting started with PHP dependency manager Composer


Hi Guys, my last article was about “Creating custom Jquery Plugin”. Today I would like to introduce you with another best project in PHP world, Composer. Today’s PHP is not like grandfather PHP, it is completely different, much more matured, elegant and ready for enterprise development. So the question is why Composer? How to manage package with composer?

Yes, composer is the great tool to manage your project dependencies, keep you updated with the dependencies. It get you the package needed for your current version and also download, manage if that package use other packages. Most of the modern frameworks are Composer powered, few examples – Cygnite PHP Framework , Zend , Symfony etc. So you are having freedom to use any third party library into your framework.

The main feature is package can be written such a way that can be usable or pluggable into any other application supported by composer.

Step1: Installing Composer

Though there is a beautiful documentation in the composer website , if you are looking for short installation and usage tutorial you can start looking at steps below.


curl -s | php

Above command will download composer.phar into your current location.

To make composer globally accessible.

sudo mv composer.phar /usr/bin/composer

This command will help you to move composer.phar into bin directory and giving you access to all composer commands with much shorter syntax.

For example:

composer.phar --version

Can be run as

composer –version

If you are Windows user you may download and run composer.exe.

Step2: Using composer.json

If you aware about the ruby (gem) or node.js (package.json) then composer.json may be familiar to you. It mostly inspired by ruby bundler and package.json. Composer uses json file to provide settings and package information for your awesome application.

Create a file into your root directory called composer.json and paste the below lines.

    "require": { 
        "cygnite/framework": "1.*"

Above example is for basic composer settings. The require keyword used for providing list of dependencies of your project. You can see in this example dependency is Cygnite Framework version 1. Cygnite Framework dependent on very few packages composer will install those automatically when you run below command into command line interface in the directory where your composer.json file exits.

 php composer.phar install
 composer install 

You can see composer is downloading Cygnite Framework and its dependencies one by one. Interesting right!

You can also see composer.lock file into your directory which is used to track all version of packages currently installed. If you change anything in composer.json file you may run below command to keep you up to date.

 composer update


Now go to the project directory and you can see vendor folder where composer installed packages. Composer also follows PSR-0 autoloader to autoload all your dependencies. You can see file called vendor/autoload.php

You can simply use autoload.php into your project and start using the package.


 require_once "vendor/autoload.php";

Step3: Sample Usage of Autoloader- Using Cygnite Asset Manager

Step2 we have installed all packages using composer, now let me show you how you can make use of those composer packages into your project.

Create a index.php file and paste below code and make sure assets exists into your application directory. Sample usage below.


require_once "vendor/autoload.php";


use Cygnite\AssetManager\AssetCollection;
use Cygnite\AssetManager\Asset;

$asset =  AssetCollection::make(function($asset)
    $asset->add('style', array('path' => 'composer-tutorial/assets/twitter/bootstrap/css/bootstrap-theme.min.css'))
                ->add('style', array('path' => 'composer-tutorial/assets/twitter/bootstrap/css/bootstrap.min.css', 'media' => '', 'title' => ''))
                ->add('style', array('path' => 'composer-tutorial/assets/css/stylesheets/*'))
                ->add('script', array('path' => 'composer-tutorial/assets/js/cygnite/jquery.js'))
                ->add('script', array('path' => 'composer-tutorial/assets/js/custom.js'))
                ->add('script', array('path' => 'composer-tutorial/assets/twitter/bootstrap/js/bootstrap.js'));

    return $asset;



You would have heard about the composer but haven’t had time to check. So I hope you have learned something today. Composer is very useful Dependency Management tool in PHP world. If I missed anything above you may also have a look at Composer official documentation.

If you find above tutorial helpful, don’t forget to give a small like or share with friends and also leave your comments below. Keep visiting.

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