1. Go to this page and download the library: Download wadakatu/laravel-spectrum 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/ */
wadakatu / laravel-spectrum example snippets
// app/Http/Requests/StoreUserRequest.php
class StoreUserRequest extends FormRequest
{
public function rules()
{
return [
'name' => '|max:120',
'roles' => 'array',
'roles.*' => 'exists:roles,id',
];
}
public function messages()
{
return [
'email.unique' => 'This email is already registered.',
];
}
}
// Controller - automatically documented!
public function store(StoreUserRequest $request)
{
$user = User::create($request->validated());
return new UserResource($user);
}
// app/Http/Resources/UserResource.php
class UserResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'email_verified' => $this->email_verified_at !== null,
'roles' => RoleResource::collection($this->whenLoaded('roles')),
'created_at' => $this->created_at->toDateTimeString(),
'profile' => new ProfileResource($this->whenLoaded('profile')),
];
}
}
// app/Transformers/UserTransformer.php
class UserTransformer extends TransformerAbstract
{
protected $availableIncludes = ['posts', 'profile', 'followers'];
protected $defaultIncludes = ['profile'];
public function transform(User $user)
{
return [
'id' => (int) $user->id,
'name' => $user->name,
'email' => $user->email,
'member_since' => $user->created_at->toDateString(),
'is_active' => (bool) $user->is_active,
];
}
public function
// Lumen Controller with inline validation
public function store(Request $request)
{
// Automatically detected and documented!
$this->validate($request, [
'title' => 'create($request->all());
return $this->fractal->item($post, new PostTransformer());
}
// config/spectrum.php
'tags' => [
// Group all authentication endpoints
'api/v1/auth/*' => 'Authentication',
// Specific endpoint mapping
'api/v1/users/profile' => 'User Profile',
// Multiple endpoints to same tag
'api/v1/orders/*' => 'Orders',
'api/v1/invoices/*' => 'Billing',
'api/v1/payments/*' => 'Billing',
],
// Automatically generates examples from your Resources
class ProductResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
'currency' => 'USD',
'in_stock' => $this->quantity > 0,
'meta' => [
'sku' => $this->sku,
'weight' => $this->weight,
],
];
}
}
// Automatic 422 validation error format
{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email field is ction render($request, Throwable $exception)
{
if ($exception instanceof ModelNotFoundException) {
return response()->json([
'error' => 'Resource not found',
'code' => 'RESOURCE_NOT_FOUND'
], 404);
}
return parent::render($request, $exception);
}
}
bash
# Generate your API documentation instantly
php artisan spectrum:generate
# Watch mode for real-time updates
php artisan spectrum:watch
bash
# Ensure FormRequest is properly type-hinted
public function store(StoreUserRequest $request) // ✅ Correct
public function store(Request $request) // ❌ Won't detect custom rules
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.