1. Go to this page and download the library: Download alexcicioc/swagger-router library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alexcicioc / swagger-router example snippets
try {
$app = new SwaggerRouter();
// Validates and extracts the information from your swagger spec
$app->use(new SpecParser('/app/swagger.json')); # Path to your spec
// Optional - Handles the /swagger endpoint that exposes the spec to frontend apps
$app->use(new SwaggerRawHandler());
// Validates the called route, http method and content type
$app->use(new RouteValidator());
// Handles extracting the parameters from the request and formatting them
$app->use(new ParamsHandler());
// Optional - Validates the OAuth2 token given in the Authorization header
$app->use(new OAuth(AuthorizationFactory::makeResourceServer()));
// Optional - Handles validating the request parameters
$app->use(new RequestValidator());
// Routes the request to it's specific controller (given by x-swagger-router-controller)
$app->use(new Router('\App\Api\Controllers')); # Controllers namespace (must be PSR-4 compliant)
// Handles formatting the response
$app->use(new ResponseHandler());
// Optional - Handles validating the response
$app->use(new ResponseValidator());
$swaggerRequest = SwaggerRequest::fromGlobals(); // extends PSR-7 ServerRequest
$swaggerResponse = new SwaggerResponse(); // extends PSR-7 Response
$response = $app($swaggerRequest, $swaggerResponse);
$response->send();
} catch (HttpException $e) {
(new SwaggerResponse())
->withStatus($e->getCode())
->body((object)['message' => $e->getMessage()])
->send();
}
namespace App\Api\Controllers;
use Alexcicioc\SwaggerRouter\SwaggerRequest;
use Alexcicioc\SwaggerRouter\SwaggerResponse;
class Courses
{
public function getCourses(SwaggerRequest $request, SwaggerResponse $response): SwaggerResponse
{
$filters = $request->getParam('technology');
$limit = $request->getParam('limit');
$startIndex = $request->getParam('startIndex');
$results = [];
// do db stuff here
return $response
->withStatus(200)
->body((object)['results' => $results]);
}
public function getCourse(SwaggerRequest $request, SwaggerResponse $response): SwaggerResponse
{
$courseId = $request->getParam('courseId');
$course = new \stdClass();
$course->id = $courseId;
// db stuff here
return $response
->withStatus(200)
->body($course);
}
}
namespace App\Http\Middleware;
use Alexcicioc\SwaggerRouter\Exceptions\HttpException;
use Alexcicioc\SwaggerRouter\Middlewares\ParamsHandler;
use Alexcicioc\SwaggerRouter\Middlewares\RequestValidator;
use Alexcicioc\SwaggerRouter\Middlewares\ResponseHandler;
use Alexcicioc\SwaggerRouter\Middlewares\ResponseValidator;
use Alexcicioc\SwaggerRouter\Middlewares\Router;
use Alexcicioc\SwaggerRouter\Middlewares\RouteValidator;
use Alexcicioc\SwaggerRouter\Middlewares\SpecParser;
use Alexcicioc\SwaggerRouter\Middlewares\SwaggerRawHandler;
use Alexcicioc\SwaggerRouter\SwaggerRequest;
use Alexcicioc\SwaggerRouter\SwaggerResponse;
use Alexcicioc\SwaggerRouter\SwaggerRouter;
use Closure;
use Illuminate\Http\Request;
class LaravelSwaggerRouter
{
public function handle(Request $request, Closure $next)
{
try {
$app = new SwaggerRouter();
// Validates and extracts the information from your swagger spec
$app->use(new SpecParser('/var/www/php/specs/spec.json')); # Path to your spec
// Optional - Handles the /swagger endpoint that exposes the spec to frontend apps
$app->use(new SwaggerRawHandler());
// Validates the called route, http method and content type
$app->use(new RouteValidator());
// Handles extracting the parameters from the request and formatting them
$app->use(new ParamsHandler());
// Optional - Handles validating the request parameters
$app->use(new RequestValidator());
// Routes the request to it's specific controller (given by x-swagger-router-controller)
$app->use(new Router('\App\Http\Controllers')); # Controllers namespace (must be PSR-4 compliant)
// Handles formatting the response
$app->use(new ResponseHandler());
// Optional - Handles validating the response
$app->use(new ResponseValidator());
$swaggerRequest = SwaggerRequest::fromGlobals();
$swaggerResponse = new SwaggerResponse(); // extends PSR-7 Response
$response = $app($swaggerRequest, $swaggerResponse);
return response()->json($response->rawBody, $response->getStatusCode(), $response->getHeaders());
} catch (HttpException $e) {
return response()->json((object)['message' => $e->getMessage()], $e->getCode());
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.