PHP code example of hcesrl / laravel-swagger-api
1. Go to this page and download the library: Download hcesrl/laravel-swagger-api 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/ */
hcesrl / laravel-swagger-api example snippets
use LaravelApi\Facade as Api;
Api::get('some-uri', 'Controller@action');
Api::post('some-uri', function () {
// do something
});
use LaravelApi\Facade as Api;
Api::get('some-uri', 'Controller@action')
->setSummary('My operation summary')
->setDescription('My operation description')
->addTag('some-tag')
->setOperationId('executeAction')
->setConsumes(['application/json'])
->setProduces(['application/json']);
use LaravelApi\Facade as Api;
Api::post ( 'post-uri', 'Controller@action' )
->addHeaderParameter ( 'header-name', 'Some description.' )
->addQueryParameter ( 'query-name', 'Some description.', true, 'integer' )
->addPathParameter ( 'path-name', 'Some description.', true, 'string' )
->addFormDataParameter ( 'formdata-name', 'Some description.', true, 'string' )
->addBodyParameter ( 'param-name', 'Some description.', true );
use LaravelApi\Facade as Api;
Api::post ( 'post-uri-2', 'Controller@action' )
->addQueryParameter ( 'param-name', function ( $param ) {
$param->setDescription ( 'Some param description' )
->setType('integer')
->setFormat('int32');
} );
use LaravelApi\Facade as Api;
Api::get('some-uri/{param1}/{param2?}', 'Controller@action');
use LaravelApi\Facade as Api;
Api::get('some-uri/{param1}/{param2?}', 'Controller@action')
->addPathParameter ( 'param1', function( $param ) {
$param->setDescription ( 'Some description' );
} )
->addPathParameter ( 'param2', 'Some other description.', false, 'integer' );
use LaravelApi\Facade as Api;
Api::get ( 'some-uri', 'Controller@action' )
->addResponse ( 200, 'Successful operation' )
->addResponse ( 422, 'Validation error' );
use LaravelApi\Facade as Api;
Api::routeParameter ( 'locale' )
->setDescription ( 'The request locale.' )
->setRequired ( true )
->addOptions ( 'en', 'it' );
Api::get ( '{locale}/some-uri', 'Controller@action' );
use LaravelApi\Facade as Api;
Api::post ( 'some-uri', 'Controller@action' )
->bindRequest ( 'App\\Http\\Requests\\MyFormRequest' );
use LaravelApi\Facade as Api;
Api::tag('simple-tag');
Api::tag('tag-with-description', 'Tag description', function() {
Api::get('tagged-uri', 'Controller@action');
});
use LaravelApi\Facade as Api;
Api::tags(
[
'tag_1' => 'Some tag description',
'tag_2' => 'Some other tag description'
]
);
use LaravelApi\Facade as Api;
/**
* /api/v1/versioned-uri
*/
Api::version('v1', function() {
Api::get('versioned-uri', 'Controller@action');
});
/**
* /api/v2/versioned-uri
*/
Api::version('v2', function() {
Api::get('versioned-uri', 'Controller@action');
});
use LaravelApi\Facade as Api;
Api::aggregate ( 'aggregate/uri', [
'App\\Page',
'App\\Post',
'settings' => function ( SettingStore $settings ) {
return $settings->all ();
},
] );
use LaravelApi\Facade as Api;
Api::models ( [
'pages' => \App\Page::class,
'users' => \App\User::class
] );
use LaravelApi\Facade as Api;
Api::resource ( 'models/pages', 'SomeController' )
->only ( 'index', 'show' );
Api::resource ( 'models/users', 'SomeController' )
->only ( 'index', 'show' );
class Foo extends Model
{
public function toApiResource ( $resource )
{
return new MyCustomResource ( $resource );
}
public function toApiResourceCollection ( $resource )
{
return new MyCustomResourceCollection ( $resource );
}
}
use LaravelApi\Facade as Api;
Api::basicAuthSecurity ( 'basic_auth' );
Api::apiKeySecurity ( 'api_key' )
->parameterName ( 'apiKey' )
->inHeader ();
Api::oauth2ImplicitSecurity ( 'oauth2_implicit' )
->authorizationUrl ( 'http://www.foobar.com' )
->description ( 'A description for the auth.' )
->setScopes ( [
'write' => 'Write something',
'read' => 'Read something',
] );
Api::oauth2PasswordSecurity ( 'oauth2_password' )
->tokenUrl ( 'http://www.foobar.com' )
->setScopes ( ... );
Api::oauth2ApplicationSecurity ( 'oauth2_application' )
->tokenUrl ( 'http://www.foobar.com' )
->setScopes ( ... );
Api::oauth2AccessCodeSecurity ( 'oauth2_accesscode' )
->tokenUrl ( 'http://www.foobar.com' )
->setScopes ( ... );
Api::get ( 'some-uri', 'Controller@action' )
->
Api::resource ( 'models/pages', 'SomeController' )
->
bash
php artisan vendor:publish --provider="LaravelApi\ServiceProvider"
bash
php artisan api:cache
bash
php artisan api:clear