w2w / laravel-apie
Laravel wrapper for w2w/apie
Installs: 4 189
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^7.2
- doctrine/annotations: ^1.7
- doctrine/cache: ^1.8
- illuminate/container: ^5.6|^6.0|^7.0
- illuminate/contracts: ^5.6|^6.0|^7.0
- illuminate/database: ^5.6|^6.0|^7.0
- illuminate/http: ^5.6|^6.0|^7.0
- illuminate/routing: ^5.6|^6.0|^7.0
- ramsey/uuid: ^3.8
- symfony/cache: ^4.4|^5.0
- symfony/options-resolver: ^4.4|^5.0
- symfony/psr-http-message-bridge: ^1.2
- symfony/serializer: ^4.4|^5.0
- w2w/apie: ^3.4.2
Requires (Dev)
- mockery/mockery: ^1.2
- nunomaduro/larastan: ^0.4.3|^0.5|^0.6.1
- orchestra/testbench: 3.6.*
Suggests
- madewithlove/illuminate-psr-cache-bridge: For caching in Laravel 5.*
- dev-master
- v4.x-dev
- 3.3.0
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.1.0
- 3.0.1
- 3.0.0.x-dev
- 3.0.0
- v2.x-dev
- 2.4.0
- 2.3.0
- 2.2.4
- 2.2.3
- 2.2.2
- 2.2.1
- 2.2.0
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-tmp
- dev-improved-eloquent-model-data-layer
- dev-feature-error-handler
- dev-feature/metadata-options
- dev-feature-bind-controller
- dev-alternate-openapi-php
- dev-feature-cache-bridge-optional
- dev-support-laravel-6
This package is auto-updated.
Last update: 2022-06-12 09:59:37 UTC
README
Laravel wrapper for the apie library
What does it do
This is a small wrapper around the library w2w/apie for Laravel. This library maps simple POPO's (Plain Old PHP Objects) to REST api calls. It is very similar to the excellent api platform library, but then for Laravel.
It also adds a class EloquentModelDataLayer to persist and retrieve api resources as Eloquent models and adds a status check to see if it can connect with the database. See the documentation of apie at https://github.com/pjordaan/apie
Forwards compatiblity Apie version 4
By default laravel-apie will still use the old 3.* serialization. In Apie version 4 this will change drastically, so a config option is added to enable the forwards compatible 4.* release.
Contents
- Installation
- Lumen integration
- Adding a a new api resource
- Automate registering classes
- Hooking in the laravel/lumen error handler
- Optimizations for production
- Versioning
- Integrate Eloquent with Apie
- Custom normalizers/value objects
- Modifying OpenAPI spec
- Use your own controllers
- Resource sub actions
- Laravel components integration
- PSR6 Cache integration
- L5-swagger integration
- Localization aware API
Installation
In your Laravel package you should do the usual steps to install a Laravel package.
composer require w2w/laravel-apie
In case you have no autodiscovery on to add W2w\Laravel\Apie\Providers\ApiResourceServiceProvider::class to your list of service providers manually.
Afterwards run the commands to publish the config to apie.php and run the migrations for the status checks.
artisan vendor:publish --provider="W2w\Laravel\Apie\Providers\ApiResourceServiceProvider"
artisan migrate
Now visit /swagger-ui to see the generated OpenApi spec. It will only contain specs for the default installed api resources, which is a check to identify your REST API and a health check resource. It will check if it can connect to the database.
Adding a new api resource
create this class in your app/ApiResources:
<?php namespace App\RestApi\ApiResources; use W2w\Lib\Apie\Annotations\ApiResource; use W2w\Lib\Apie\Plugins\Core\DataLayers\NullDataLayer; /** * @ApiResource(disabledMethods={"get"}, persistClass=NullDataLayer::class) */ class SumExample { private $one; private $two; public function __construct(float $one, float $two) { $this->one = $one; $this->two = $two; } public function getOne(): float { return $this->one; } public function getTwo(): float { return $this->two; } public function getAddition(): float { return $this->one + $this->two; } public function getSubtraction(): float { return $this->one - $this->two; } public function getMultiplication(): float { return $this->one * $this->two; } public function getDivison(): ?float { // === and == can fail because of floating points.... if (abs($this->two) < 0.000001) { return null; } return $this->one / $this->two; } }
Now in config/apie.php we should add the class to add it to the api resources:
<?php //config/apie.php use App\ApiResources\SumExample; use W2w\Lib\Apie\Plugins\ApplicationInfo\ApiResources\ApplicationInfo; use W2w\Lib\Apie\Plugins\StatusCheck\ApiResources\Status; return [ 'resources' => [ApplicationInfo::class, Status::class, SumExample::class] ];
If you refresh /api/doc.json you can see you get an extra POST call to create a SumExample resource. With any OpenApi tool or with Postman you can test the POST command. If you would make a POST call to /api/sum_example with body
{
"one": 13,
"two": 1
}
You would get:
{
"one": 13,
"two": 1,
"addition": 14,
"subtraction": 12,
"multiplication": 13,
"divison": 13
}
Automate registering api resources.
It is possible to automate registering api resources without having to manually update the resources list in config/apie.php We can auto-register all classes in a specific namespace with this:
- In a terminal run:
composer require haydenpierce/class-finder
- Open config/apie.php
- Edit the file like this:
<?php //config/apie.php use W2w\Lib\Apie\Core\Resources\ApiResourcesFromNamespace; return [ 'resources' => ApiResourcesFromNamespace::createApiResources('App\RestApi\ApiResources'), ];
Now if I put a class inside the namespace App\RestApi\ApiResources, the class will be registered for Apie.
Make sure that for production you use laravel's config cache to reduce load on your server.