PHP Classes

Laravel OpenAPI API Application Bridge: Implement API using Laravel specified with OpenAPI

Recommend this page to a friend!
  Info   View files Documentation   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog (1)    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 31 This week: 1All time: 11,009 This week: 560Up
Version License PHP version Categories
application-bridge-l 1.0.0GNU General Publi...5PHP 5, Libraries, Web services
Description 

Author

This package provides means to implement an API using Laravel specified with OpenAPI.

It provides an application bridge class that can load an API specification defined using an OpenAPI file.

The class adds routes according to the API endpoints defined in the OpenAPI, so developers can implement controllers that implement the respective API actions.

Innovation Award
PHP Programming Innovation award winner
April 2022
Winner


Prize: One subscription to the PDF edition of the PHP Architect magazine
OpenAPI is a standard that defines a file format that uses JSON to describe the functionality of an API.

Developers that want to use OpenAPI to implement an API need to parse the API specification in JSON format and write code that implements the functions of each API endpoint.

This package makes it easier for Laravel developers to implement APIs based on the OpenAPI specification. It can process an OpenAPI specification to add routes for the API endpoints.

This way, the developers only need to develop the code for the action controllers of each API endpoint.

Manuel Lemos
Picture of Virgilio lino
  Performance   Level  
Name: Virgilio lino <contact>
Classes: 8 packages by
Country: Italy Italy
Age: 45
All time rank: 157764 in Italy Italy
Week rank: 411 Up18 in Italy Italy Up
Innovation award
Innovation award
Nominee: 5x

Winner: 2x

Documentation

Open-api

Maybe it's about automation, or just about being more declarative because a non Touring complete DSL will just more be correct, I find it just amazing the possibility to describe an API by using the Open-API-Specification and let this specification be your code: this class will set every route using the Slim functionalities, and for every route point to a CommandHandler.

I'd suggest, the best way to see it in action is just to clone the repository and try the Example Hello World Application:

git clone git@github.com:virgiliolino/open-api.git
cd SwaggerSlimDispatcher/Examples/HelloWorld/
composer install   #composer install will actually install Slim and open-api
php -S localhost:8080 -t public public/index.php #start the server
curl localhost:8080/hello/world # or just open the browser localhost:8080/hello/world

For a fully working application, you could take a look at a ReactJS + Slim Skeleton that provides all the functionalities needed for a modern application. The url is here The OpenApi specification is here

You will not have a few overpopulated Controllers, but instead for every entry point a command handler. You can read at this blog post for some ideas of how we intend our architecutre

Furthermore, the Open-Api specification can be automatically validated, tested Swagger

In the end you'll have a yml or json file that describe your API, something like this: Json Specification

By using our library all routes will automatically be set. Every route pointing to a CommandHandler indicated by a unique operationId. So in the image of the example, you can see that there is a route: /pet that accept post requests. It will be enough to use our class, when you start the application the route /pet will accept a post. And so for the gets that you see below, like /pet/findByStatus, etc. For every path, it will be executed the command handler with the operationI. In the example for /pet, you can see the operationId: addPet. So making a post request to /pet, the system will try to execute the class AddPet::execute passing the params. The operationId must be a fully qualified name of a class. Something like this for example: operationId: \MyApplication\CommandHandlers\AddPett which means that will execue AddPett::execute

You may find an example of a fully working Open-Api specification here the full json file

Installation

Even if its working, I'd not consider it really a stable package. So to install it you need to proceed in that way:

 composer require dispatcher/swagger-dispatcher dev-master

Examples/Helloworld

$app = new \Slim\App;
$container = $app->getContainer();
$container['HelloWorld'] = function () {
    return new \HelloWorld\CommandHandlers\HelloWorld();
};
$openApiFile = 'routes.json';
$openApiConfigParser = Bridge\ApplicationBridge\ParserFactory::parserFor($openApiFile);
$openApiConfig = $openApiConfigParser->parse($openApiFile);
\Bridge\ApplicationBridge\OpenApiDispatcher::InjectRoutesFromConfig($app, $openApiConfig);
$app->run();

As you may see we're injecting HelloWorld, a command Handler with the same id of operationId that you may find on routes.json

That's all folks.

Help wanted

There is no validation at all. This process can be automatized. Class CommandHandler on the file called SwaggerDispatcher.

Thanks, Virgilio


  Files folder image Files  
File Role Description
Files folder imageLab (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Image file swagger.png Data Auxiliary data

  Files folder image Files  /  Lab  
File Role Description
Files folder imageApplication (1 directory)

  Files folder image Files  /  Lab  /  Application  
File Role Description
Files folder imageBridge (1 file)

  Files folder image Files  /  Lab  /  Application  /  Bridge  
File Role Description
  Plain text file LaravelBridge.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:31
This week:1
All time:11,009
This week:560Up