Skip to content

Instantly share code, notes, and snippets.

@raank
Created June 1, 2021 14:06
Show Gist options
  • Save raank/d542857269e318a7789be4c40922cd51 to your computer and use it in GitHub Desktop.
Save raank/d542857269e318a7789be4c40922cd51 to your computer and use it in GitHub Desktop.
<?php
# File: project/routes/api.php
require_once __DIR__.'/../vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Instance Environment Variables.
|--------------------------------------------------------------------------
*/
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__)
))->bootstrap();
/*
|--------------------------------------------------------------------------
| Define constants to Documentation.
|--------------------------------------------------------------------------
| Define yours variables like a constants
| to using with Swagger documentation.
*/
define('APP_URL', env('APP_URL'));
define('APP_NAME', env('APP_NAME'));
/**
* @OA\Info(
* title=APP_NAME,
* description="This is a documentation of my first project.",
* version="1.0.",
* @OA\Contact(
* email="your@email.com"
* )
* )
*
* @OA\Schemes(format={"https", "http"})
* @OA\Server(url=APP_URL)
*
* @OA\Tag(name="crud", description="My Crud Tag")
*
* @OA\SecurityScheme(
* securityScheme="bearerAuth",
* type="http",
* scheme="bearer"
* )
*/
openapi: 3.0.0
info:
title: Lumen
description: 'This is a documentation of my first project.'
contact:
email: your@email.com
version: 1.0.
servers:
-
url: 'http://localhost:8181'
paths:
/api/v1/users:
get:
tags:
- crud
summary: 'Listing all resources.'
operationId: 'App\Http\Controllers\UsersController::index'
responses:
'200':
description: 'Response Successful'
content:
application/json:
schema:
properties:
message: { description: 'Listing all resources.', type: string, example: 'Successful action!' }
data: { description: 'Listing all resources.', type: array, items: { $ref: '#/components/schemas/User' } }
type: object
default:
description: 'Response Error'
post:
tags:
- crud
summary: 'Storing a new resource.'
operationId: 'App\Http\Controllers\UsersController::store'
requestBody:
content:
application/json:
schema:
properties:
name:
description: 'The name of user.'
type: string
example: 'John Doe'
email:
description: 'The email of user.'
type: string
example: john@doe.com
type: object
responses:
'200':
description: 'Response Successful'
content:
application/json:
schema:
properties:
message: { description: 'Storing a new resource.', type: string, example: 'Successful action!' }
data: { $ref: '#/components/schemas/User' }
type: object
default:
description: 'Response Error'
'/api/v1/users/{id}':
get:
tags:
- crud
summary: 'Show a specific resource.'
operationId: 'App\Http\Controllers\UsersController::show'
parameters:
-
name: id
in: path
description: 'Identification of User'
required: true
schema:
type: integer
example: 1
responses:
'200':
description: 'Response Successful'
content:
application/json:
schema:
properties:
message: { description: 'Show a specific resource.', type: string, example: 'Successful action!' }
data: { $ref: '#/components/schemas/User' }
type: object
default:
description: 'Response Error'
put:
tags:
- crud
summary: 'Updating a specific resource.'
operationId: 'App\Http\Controllers\UsersController::update'
parameters:
-
name: id
in: path
description: 'Identification of User'
required: true
schema:
type: integer
example: 1
requestBody:
content:
application/json:
schema:
properties:
name:
description: 'The name of user.'
type: string
example: 'John Doe'
email:
description: 'The email of user.'
type: string
example: john@doe.com
type: object
responses:
'200':
description: 'Response Successful'
content:
application/json:
schema:
properties:
message: { description: 'Updating a specific resource.', type: string, example: 'Successful action!' }
data: { description: 'Updating a specific resource.', type: boolean, example: true }
type: object
default:
description: 'Response Error'
delete:
tags:
- crud
summary: 'Deleting a specific resource'
operationId: 'App\Http\Controllers\UsersController::destroy'
parameters:
-
name: id
in: path
description: 'Identification of User'
required: true
schema:
type: integer
example: 1
responses:
'200':
description: 'Response Successful'
content:
application/json:
schema:
properties:
message: { description: 'Deleting a specific resource', type: string, example: 'Successful action!' }
data: { description: 'Deleting a specific resource', type: boolean, example: true }
type: object
default:
description: 'Response Error'
components:
schemas:
User:
description: 'Schema of Model User'
properties:
id:
description: 'Identification of User'
type: integer
example: 1
name:
description: 'Name of User'
type: string
example: 'John Doe'
email:
description: 'Email of User'
type: string
example: john@doe.com
type: object
securitySchemes:
bearerAuth:
type: http
scheme: bearer
tags:
-
name: crud
description: 'My Crud Tag'
<?php
namespace App\Models;
/**
* @OA\Schema(
* schema="User",
* type="object",
* description="Schema of Model User",
* @OA\Property(
* property="id",
* type="integer",
* description="Identification of User",
* example=1
* ),
* @OA\Property(
* property="name",
* type="string",
* description="Name of User",
* example="John Doe"
* ),
* @OA\Property(
* property="email",
* type="string",
* description="Email of User",
* example="john@doe.com"
* )
* )
*/
class User extends \Framework\Package\Model
{
/**
* The identification of this resource.
*
* @var integer
*/
public $id;
/**
* The name of this current user.
*
* @var string
*/
public $name;
/**
* The email of this current email.
*
* @var string
*/
public $email;
}
<?php
namespace App\Http\Controllers;
class UsersController extends \App\Http\Controller
{
/**
* Listing all resources.
*
* @OA\Get(
* path="/api/v1/users",
* tags={"crud"},
* @OA\Response(
* response=200,
* description="Response Successful",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="message",
* type="string",
* example="Successful action!"
* ),
* @OA\Property(
* property="data",
* type="array",
* @OA\Items(ref="#/components/schemas/User")
* )
* )
* )
* ),
* @OA\Response(
* response="default",
* description="Response Error"
* )
* )
*
* @return array
*/
public function index(): array
{
return [
'message' => 'Successful action!',
'data' => \App\Models\User::all()
];
}
/**
* Storing a new resource.
*
* @OA\Post(
* path="/api/v1/users",
* tags={"crud"},
* @OA\Response(
* response=200,
* description="Response Successful",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="message",
* type="string",
* example="Successful action!"
* ),
* @OA\Property(
* property="data",
* type="object",
* ref="#/components/schemas/User"
* )
* )
* )
* ),
* @OA\Response(
* response="default",
* description="Response Error"
* ),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="name",
* type="string",
* description="The name of user.",
* example="John Doe"
* ),
* @OA\Property(
* property="email",
* type="string",
* description="The email of user.",
* example="john@doe.com"
* )
* )
* )
* )
* )
*
* @return array
*/
public function store(array $data): array
{
return [
'message' => 'Successful action!',
'data' => \App\Models\User::create([
'name' => 'John Doe',
'email' => 'john@doe.com'
])
];
}
/**
* Show a specific resource.
*
* @OA\Get(
* path="/api/v1/users/{id}",
* tags={"crud"},
* @OA\Parameter(
* name="id",
* in="path",
* required=true,
* description="Identification of User",
* example=1,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response=200,
* description="Response Successful",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="message",
* type="string",
* example="Successful action!"
* ),
* @OA\Property(
* property="data",
* type="object",
* ref="#/components/schemas/User"
* )
* )
* )
* ),
* @OA\Response(
* response="default",
* description="Response Error"
* )
* )
*
* @param int $id
* @return array
*/
public function show(int $id): array
{
return [
'message' => 'Successful action!',
'data' => \App\Models\User::find($id)
];
}
/**
* Updating a specific resource.
*
* @OA\Put(
* path="/api/v1/users/{id}",
* tags={"crud"},
* @OA\Parameter(
* name="id",
* in="path",
* required=true,
* description="Identification of User",
* example=1,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response=200,
* description="Response Successful",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="message",
* type="string",
* example="Successful action!"
* ),
* @OA\Property(
* property="data",
* type="boolean",
* example=true
* )
* )
* )
* ),
* @OA\Response(
* response="default",
* description="Response Error"
* ),
* @OA\RequestBody(
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="name",
* type="string",
* description="The name of user.",
* example="John Doe"
* ),
* @OA\Property(
* property="email",
* type="string",
* description="The email of user.",
* example="john@doe.com"
* )
* )
* )
* )
* )
*
* @param int $id
* @param array $data
* @return array
*/
public function update(int $id, array $data): array
{
return [
'message' => 'Successful action!',
'data' => \App\Models\User::find($id)->update($data)
];
}
/**
* Deleting a specific resource
*
* @OA\Delete(
* path="/api/v1/users/{id}",
* tags={"crud"},
* @OA\Parameter(
* name="id",
* in="path",
* required=true,
* description="Identification of User",
* example=1,
* @OA\Schema(
* type="integer"
* )
* ),
* @OA\Response(
* response=200,
* description="Response Successful",
* @OA\MediaType(
* mediaType="application/json",
* @OA\Schema(
* @OA\Property(
* property="message",
* type="string",
* example="Successful action!"
* ),
* @OA\Property(
* property="data",
* type="boolean",
* example=true
* )
* )
* )
* ),
* @OA\Response(
* response="default",
* description="Response Error"
* )
* )
*
* @param int $id
* @return array
*/
public function destroy(int $id): array
{
return [
'message' => 'Successful action!',
'data' => \App\Models\User::delete($id)
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment