Apigility Project Setup

How to Start an apigility project -

Step 1:
    Install xamp server and do all the setup
Step 2:
    Download an apigility skeleton project from web and place it in xamp/htdocs folder (Apigility Skeleton)
Step 3:
    Configure the Apigility skeleton project folder path in vhosts file(To create a web server you need to do this setup) Ex: you setup the server Url like 192.168.1.244:8080
Step 4:
    192.168.1.250:8080 give this Url in the Chrom browser, you can see an apigility UI, Click on database tab and add your database connectivity info and click on new API
Step 5:
    After creating an API click on Create new service.
Step 6:
    Select REST tab and give your Web Service name and press Create Service Button - Now you just create an API and service in that API
Step 7:
    Once you created a service there will be four files which created automatically with the service name as folder
        Ex: Service name is 'UserLogin' then in the path /var/www/html/android/foldername/module/apiname/src/apiname/UserLogin will be created automatically
            Along with the UserLogin folder there will be four files
            1. Collection class (UserLoginCollection.php)
            2. Entity class (UsrLoginEntitiy.php)
            3. Resource class (UserloginResource.php)
            4. ResourceFactory class (UserLoginResourceFactory.php)

Additionally we need to create a mapper file for functionality implementation like UserLoginMapper.php
Here we need to add newly created Mapper class information in the factories array
        Ex:
        'sample\V1\Rest\UserLogin\UserLoginMapper' => function ($sm) {
                            $adapter = $sm->get ( 'Zend\Db\Adapter\Adapter' );
                            return new \sample\V1\Rest\UserLogin\UserLoginMapper ( $adapter );
        },
Note: Every time you are creating a new service you should add this Mapper information to the path /var/www/html/android/sampleapp/module/sample/src/sample

Ex:
UserLoginResourceFactory.php

<?php
namespace cabbooking\V1\Rest\UserLogin;

class UserLoginResourceFactory
{

    public function __invoke ($services)
    {
        $mapper = $services->get('cabbooking\V1\Rest\UserLogin\UserLoginMapper');
        return new UserLoginResource($mapper);
    }
}

UserLoginResource.php

<?php
        namespace cabbooking\V1\Rest\UserLogin;
        use ZF\ApiProblem\ApiProblem;
        use ZF\Rest\AbstractResourceListener;

        class UserLoginResource extends AbstractResourceListener
        {

            //We need to create constructor which initializes the Mapper class (Added newly)
            protected $mapper;

            public function __construct ($mapper)
            {
                $this->mapper = $mapper;
            }
            //Added newly
           
            /**
             * Create a resource
             *
             * @param mixed $data
             * @return ApiProblem|mixed
             */
            public function create ($data)
            {
                //This line will call your Mapper method(Whetever the functionality you need to done in the post method you handle here)
                return $this->mapper->userLogin($data);
            }

            /**
             * Delete a resource
             *
             * @param mixed $id
             * @return ApiProblem|mixed
             */
            public function delete ($id)
            {
                return new ApiProblem(405,
                        'The DELETE method has not been defined for individual resources');
            }

            /**
             * Delete a collection, or members of a collection
             *
             * @param mixed $data
             * @return ApiProblem|mixed
             */
            public function deleteList ($data)
            {
                return new ApiProblem(405,
                        'The DELETE method has not been defined for collections');
            }

            /**
             * Fetch a resource
             *
             * @param mixed $id
             * @return ApiProblem|mixed
             */
            public function fetch ($id)
            {
                return new ApiProblem(405,
                        'The GET method has not been defined for individual resources');
            }

            /**
             * Fetch all or a subset of resources
             *
             * @param array $params
             * @return ApiProblem|mixed
             */
            public function fetchAll ($params = array())
            {
                return new ApiProblem(405,
                        'The GET method has not been defined for collections');
            }

            /**
             * Patch (partial in-place update) a resource
             *
             * @param mixed $id
             * @param mixed $data
             * @return ApiProblem|mixed
             */
            public function patch ($id, $data)
            {
                return new ApiProblem(405,
                        'The PATCH method has not been defined for individual resources');
            }

            /**
             * Replace a collection or members of a collection
             *
             * @param mixed $data
             * @return ApiProblem|mixed
             */
            public function replaceList ($data)
            {
                return new ApiProblem(405,
                        'The PUT method has not been defined for collections');
            }

            /**
             * Update a resource
             *
             * @param mixed $id
             * @param mixed $data
             * @return ApiProblem|mixed
             */
            public function update ($id, $data)
            {
                return new ApiProblem(405,
                        'The PUT method has not been defined for individual resources');
            }
        }

UserLoginMapper.php

<?php

        namespace cabbooking\V1\Rest\UserLogin;

        use Zend\Db\Sql\Select;
        use Zend\Db\Sql\Sql;
        use Zend\Db\Adapter\Adapter;
        use Zend\Db\Adapter\AdapterInterface;
        use Zend\Paginator\Adapter\DbSelect;

        class UserLoginMapper {
            protected $adapter;
            protected $tblName = 'user_login';
            public function __construct(AdapterInterface $adapter) {
                $this->adapter = $adapter;
            }
           
            // Create an field type
            public function userLogin($data) {
                $data = ( array ) ($data);
                if($data ['userName'] != null && $data ['password'] != null) {
                    $sql = 'SELECT * FROM ' . $this->tblName . ' WHERE (email_id= ? OR mobile_no= ?) AND  BINARY password= ?';
                    $resultset = $this->adapter->query ( $sql, array (
                            $data ['userName'],
                            $data ['userName'],
                            $data ['password']
                    ) );
                    $data1 = $resultset->toArray ();
                   
                    $rows = $resultset->count ();
                    if ($rows > 0) {
                            $userdetails = array (
                                    'userId' => $data1 [0] ["user_id"],
                                    'firstName' => $data1 [0] ["first_name"],
                                    'lastName' => $data1 [0] ["last_name"],
                                    'gender' => $data1 [0] ["gender"],
                                    'mobileNo' => $data1 [0] ["mobile_no"],
                                    'emailId' => $data1 [0] ["email_id"],
                                    'modifiedDate' => $data1 [0] ['last_modified_date']
                            );
                           
                            $arr = array (
                                    'status' => 1,
                                    'result' => $userdetails
                            );
                            echo json_encode ( $arr );
                           
                            die ();
                    } else {
                        $arr = array (
                                'status' => 0,
                                'errorMessage' => "No Matching Guest"
                        );
                        echo json_encode ( $arr );
                        die ();
                    }
                } else {
                    $arr = array (
                                'status' => 0,
                                'errorCode' => "missing.requiredFields",
                                'errorMessage' => "Required field(s) missing"
                        );
                        echo json_encode ( $arr );
                        die ();
                }
            }
        }

Comments

Popular posts from this blog

Creating a .ssh key in window and Mac machines

Git Solutions