- Notifications
You must be signed in to change notification settings - Fork 1k
Open
Description
I use webman framework. Here is the note to integrate with webman, maybe someone will need it.
composer require mevdschee/php-crud-api
create ApiController.php in app/controller
namespace app\controller; use Tqdev\PhpCrudApi\Api; use Tqdev\PhpCrudApi\Config\Config; class ApiController { public function index($request, $any = '') { $config = new Config([ 'driver' => 'sqlite', 'address' => 'xxxx], 'basePath' => '/api', 'middlewares' => 'apiKeyAuth, pageLimits', 'apiKeyAuth.keys' => env('API_KEY'), 'debug' => false, ]); $api = new Api($config); // Create ServerRequest from PHP globals and override path $requestFactoryClass = '\Tqdev\PhpCrudApi\RequestFactory'; if (class_exists($requestFactoryClass)) { $serverRequest = $requestFactoryClass::fromGlobals(); // Override path and query string for correct route $queryString = http_build_query($request->get()); $uri = $serverRequest->getUri() ->withPath('/api/' . $any) ->withQuery($queryString); $serverRequest = $serverRequest->withUri($uri); // Override method for ServerRequest $serverRequest = $serverRequest->withMethod($request->method()); // Pass X-API-Key header from Webman to ServerRequest $apiKey = $request->header('X-API-Key'); if ($apiKey) { $serverRequest = $serverRequest->withHeader('X-API-Key', $apiKey); } // Override body for PUT/PATCH/POST/DELETE methods $rawBody = $request->rawBody(); if ($rawBody) { $streamFactory = new \Nyholm\Psr7\Factory\Psr17Factory(); $bodyStream = $streamFactory->createStream($rawBody); $serverRequest = $serverRequest->withBody($bodyStream); } } else { throw new \Exception('RequestFactory class not found'); } $response = $api->handle($serverRequest); // Returns a response that is in accordance with Webman standards $body = $response->getBody(); $status = $response->getStatusCode(); $headers = $response->getHeaders(); unset($headers['Content-Length']); return response($body, $status, $headers); } add in route.php
Route::any('/api/{any:.+}', [app\controller\ApiController::class, 'index']);
Metadata
Metadata
Assignees
Labels
No labels