hkulekci / expressive-api-component
API Component
Installs: 43
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^7.2
- fig/http-message-util: ^1.1
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- roave/security-advisories: dev-master
- zendframework/zend-config-aggregator: ^0.2.0
- zendframework/zend-expressive: ^3.0
- zendframework/zend-filter: ^2.7
- zendframework/zend-inputfilter: ^2.7
- zendframework/zend-json: ^3.0
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is not auto-updated.
Last update: 2024-10-27 06:18:19 UTC
README
This library solves spesific problems. Not a general solution for APIs. There are helper classes, traits and filters.
Install with composer :
composer require hkulekci/expressive-api-component
Add the middleware to your config file :
'middleware_pipeline' => [
'always' => [
'middleware' => [
.....
],
'priority' => 10000,
],
'routing' => [
'middleware' => [
Zend\Expressive\Router\Middleware\RouteMiddleware::class,
Zend\Expressive\Helper\UrlHelperMiddleware::class,
....
ApiComponent\Helper\ApiMiddleware::class,
ApiComponent\RequestDataParser::class,
....
Zend\Expressive\Router\Middleware\DispatchMiddleware::class,
\Zend\Stratigility\Middleware\NotFoundHandler::class,
],
'priority' => 1,
],
'notFound' => [
'middleware' => 'Application\NotFound',
'priority' => -1000,
]
],
Then use ApiComponent\AbstractResource
class to extends for your API endpoint resource like below:
<?php
/**
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/
namespace CoreApi\Common;
use ApiComponent\AbstractResource;
use ApiComponent\Helper\ApiResponse;
use ApiComponent\Helper\ApiResponseInterface;
class PingAction extends AbstractResource
{
/**
* Ping Service
*
* @SWG\Get(
* path="/ping",
* tags={"common"},
* @SWG\Response(response="200", description="Ping Service"),
* @SWG\Response(response="500", description="Internal Server Error"),
* )
*
* @param array $data
*
* @return ApiResponseInterface
*/
protected function fetchAll(array $data = []): ApiResponseInterface
{
return new ApiResponse(['service' => $data['service']]);
}
}
For filtering your input create a route middleware and extend ApiComponent\AbstractInputFilter
class like below:
<?php
/**
* @author Haydar KULEKCI <haydarkulekci@gmail.com>
*/
namespace CoreApi\Common;
use ApiComponent\AbstractInputFilter;
class PingActionInputFilter extends AbstractInputFilter
{
protected function inputFilterSpecsForFetchAllParameters(): array
{
return [
$this->string('service', true, ['min' => 3, 'max' => 255]),
];
}
}
Now, your should router config should be like below :
[
'name' => 'coreapi.common.ping',
'path' => '/ping',
'middleware' => [
\CoreApi\Common\PingActionInputFilter::class,
\CoreApi\Common\PingAction::class,
],
'allowed_methods' => ['GET'],
],