PHPackages                             ankitgs/restapi - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. ankitgs/restapi

AbandonedLibrary[HTTP &amp; Networking](/categories/http)

ankitgs/restapi
===============

Zend Framework 3 module to build REST APIs

4.1(9y ago)266MITPHP

Since May 9Pushed 9y agoCompare

[ Source](https://github.com/ankitsolanki-multidots/restapi)[ Packagist](https://packagist.org/packages/ankitgs/restapi)[ Docs](https://github.com/multidots/zf3-rest-api)[ RSS](/packages/ankitgs-restapi/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (5)Dependencies (2)Versions (6)Used By (0)

RestApi plugin for Zend Framework 3
===================================

[](#restapi-plugin-for-zend-framework-3)

Requirements
------------

[](#requirements)

This plugin has the following requirements:

- Zend Framework 3 or greater.
- PHP 7 or greater.

Installation
------------

[](#installation)

You can install this plugin into your Zend Framework application using [composer](http://getcomposer.org).

The recommended way to install composer packages is:

```
composer require multidots/zf3-restapi

```

After installation, go to root path and open composer.json file and add following.

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

Now Execute following command

```
composer dump-autoload
```

Now copy this file "vender/restapi/config/restapi.global.php" and paste to root "config/autoload/restapi.global.php"

Now add this 'restapi' to modules.config.php file.

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

Usage
-----

[](#usage)

You just need to create your API related controller and extend it to `ApiController` instead of default `AbstractActionController`. You just need to set you results in `apiResponse` variable and your response code in `httpStatusCode` variable and return $this-&gt;createResponse(). For example,

```
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();
    }
}
```

You can define your logic in your action function as per your need. For above example, you will get following response in `json` format,

```
{"status":"OK","result":{"you_response":"your response data"}}
```

The URL for above example will be `http://yourdomain.com/foo/bar`. You can customize it by setting the your module.config.php as following`.

```
'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.
                    ],
                ],
            ],
        ],
    ],
```

Simple :)

Configurations
--------------

[](#configurations)

This plugin provides several configuration related to Response, Request and `JWT` authentication. The default configurations are in previously you copy and past file this restapi.global.php have configurations`.

```
