PHP code example of ankitgs / restapi

1. Go to this page and download the library: Download ankitgs/restapi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ankitgs / restapi example snippets


"autoload": {
        "psr-4": {
            ....
            // add following line.
            "restapi\\": "vendor/restapi/src/"
        }
    },

composer dump-autoload

return [
    ....
    //add this 
    'restapi'
];

namespace Application\Controller;

use restapi\Controller\ApiController;

/**
 * Foo Controller
 */
class FooController extends ApiController
{

    /**
     * bar method
     *
     */
    public function barAction()
    {
		// your action logic

		// Set the HTTP status code. By default, it is set to 200
		$this->httpStatusCode = 200;

		// Set the response
		$this->apiResponse['you_response'] = 'your response data';

                return $this->createResponse();
    }
}

'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\FooController::class,
                        'action'     => 'bar',
                        'isAuthorizationRequired'    => true // set true if this api Required JWT Authorization.
                    ],
                ],
            ],
        ],
    ],



return [
    'ApiRequest' => [
        'responseFormat' => [
            'statusKey' => 'status',
            'statusOkText' => 'OK',
            'statusNokText' => 'NOK',
            'resultKey' => 'result',
            'messageKey' => 'message',
            'defaultMessageText' => 'Empty response!',
            'errorKey' => 'error',
            'defaultErrorText' => 'Unknown request!',
            'authenticationRequireText' => 'Authentication Required.',
            'pageNotFoundKey' => 'Request Not Found.',
        ],
        'jwtAuth' => [
            'cypherKey' => 'R1a#2%dY2fX@3g8r5&s4Kf6*sd(5dHs!5gD4s',
            'tokenAlgorithm' => 'HS256'
        ],
    ]
];

'router' => [
        'routes' => [
            'home' => [
                'type' => Literal::class,
                'options' => [
                    'route'    => '/',
                    'defaults' => [
                        'controller' => Controller\FooController::class,
                        'action'     => 'bar',
                        'isAuthorizationRequired'    => true // set true if this api Required JWT Authorization.
                    ],
                ],
            ],
        ],
    ],

Authorization: Bearer [token]

Example:
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjoidmFsdWUifQ.xQgVrW5o3iNzA4iY3NDKwDp-pYoIhr-vyFgmi9RuMFo

    public function login()
    {
        /**
         * process your data and validate it against database table
         */

        // generate token if valid user
        // $this->tokenPayload you can access user details.
        $this->tokenPayload = ['email' => $user->email, 'name' => $user->name];
        $this->generateToken();

        // $this->token through you can get token.
        $this->apiResponse['token'] = $this->token;
        $this->apiResponse['message'] = 'Logged in successfully.';
        return $this->createResponse();
    }



namespace Application\Controller;

use restapi\Controller\ApiController;

/**
 * Articles Controller
 *
 * 
 */
class ArticlesController extends ApiController
{

    /**
     * index method
     *
     */
    public function indexAction()
    {
        $articles = $this->entityManager->getRepository(Article::class)
                ->findBy([], ['id'=>'ASC']);

        $this->apiResponse['articles'] = $articles;
    }
}