nhattuanbl / lara-swagger
Swagger ui doc for laravel
Requires
- php: ^8.0
- illuminate/support: *
- nhattuanbl/lara-helper: dev-master
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-17 09:05:17 UTC
README
Swagger ui doc for laravel 6,7 or 8
Requires
- PHP:
>=8.0
- Laravel:
latest version 6, 7, or 8
Installation
composer require nhattuanbl/lara-swagger
php artisan vendor:publish --provider="LaraSwagger\LaraSwaggerServiceProvider" --tag="views"
Edit:
config/app.php
'providers' => [ ... \LaraSwagger\LaraSwaggerServiceProvider::class, ];
Usage
- Example route:
Route::get('/', function () { return view('vendor/lara_swagger/lara_swagger'); });
Route::get('/swagger.json', function (\LaraSwagger\LaraSwagger $laraSwagger) {
return response($laraSwagger->getDocs())->header('Content-Type', 'application/json');
});
Edit: `resources/views/vendor/lara_swagger/lara_swagger.blade.php`
window.onload = function() {
// Begin Swagger UI call region
const ui = SwaggerUIBundle({
url: "{{ url('swagger.json') }}", // <=== change to your route
dom_id: '#swagger-ui',
deepLinking: true,
- Create model and controller:
php artisan make:model Product --controller --resource
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use LaraSwagger\Attributes\LaraSwagger;
[LaraSwagger]
class Product extends Model {
use HasFactory;
}
![img](https://i.imgur.com/b1dwopy.jpg)
<details>
<summary>Parameters</summary>
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use LaraSwagger\Attributes\Parameter; use LaraSwagger\Attributes\LaraSwagger;
/**
- @property string name
@property int quantity */
[
LaraSwagger( // controller: CustomController::class, // tag: 'Product', // description: string|['get' => 'markdown support']
hidden: ['isInStock'], readOnly: ['id'],
), Parameter(name: 'page', type: 'integer', required: true, default: 1, description: 'Pagination', minimum: 1), Parameter(name: 'soldOut', type: 'boolean'), Parameter(name: 'status', type: 'string', enum: ['pending', 'delivered']), ] class Product extends Model { use HasFactory;
protected $fillable = [
'email'
];
protected $casts = [
'quantity' => 'int'
];
protected $hidden = [
'hasCategory'
];
protected $dates = [
'created_at'
];
public function isInStock(): bool {
return false;
}
public function hasCategory(): bool {
return true;
} }
![img](https://i.imgur.com/JfWYz1G.png) </details>