PHP code example of codewithagents / openapi-laravel
1. Go to this page and download the library: Download codewithagents/openapi-laravel 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/ */
codewithagents / openapi-laravel example snippets
// generated from components.schemas.Customer
final class CustomerData extends Data
{
public function __construct(
public readonly int $id,
public readonly string $name,
#[MapName('email_address')]
public readonly ?string $emailAddress = null,
public readonly ?CustomerStatus $status = null,
) {}
public static function rules(): array
{
return [
'id' => ['
// generated: app/Http/Controllers/Api/AbstractPetController.php
// (this Pet schema marks some fields readOnly/writeOnly, so the request type is
// the write variant PetWritableData and the response type is the read variant
// PetData; a schema with no such flags would use a single PetData both ways)
abstract class AbstractPetController
{
// clean RESTful operations get the conventional Laravel method names;
// anything ambiguous or non-CRUD keeps its operationId-derived name
abstract public function store(PetWritableData $pet): PetData;
abstract public function show(int $petId): PetData;
abstract public function destroy(int $petId): void; // the spec declares 204: nothing to return
}
// generated: routes/api.generated.php
// (the spec declares 201 for the create and 204 for the delete, so those
// routes carry the inlined status middleware; plain 200 operations stay untouched)
Route::post('/pet', [PetController::class, 'store'])->name('store')->middleware(RespondsWithStatus::class.':201');
Route::get('/pet/{petId}', [PetController::class, 'show'])->name('show');
Route::delete('/pet/{petId}', [PetController::class, 'destroy'])->name('destroy')->middleware(RespondsWithStatus::class.':204');
final class PetData extends Data
{
public function __construct(
public readonly string $name,
/** @var array<int, string> */
public readonly array $photoUrls,
public readonly ?int $id = null,
public readonly ?CategoryData $category = null, // nested $ref
/** @var array<int, TagData> */
#[DataCollectionOf(TagData::class)]
public readonly ?array $tags = null, // typed collection
public readonly ?string $status = null,
#[MapName('microchip_id')]
public readonly ?string $microchipId = null, // wire name mapping
#[MapName('created_at')]
public readonly ?string $createdAt = null, // readOnly: read shape only
#[MapName('weight_kg')]
public readonly ?float $weightKg = null, // nullable number
/** @var array<string, string> */
public readonly ?array $attributes = null, // additionalProperties map
/** @var string|int */
#[MapName('external_id')]
public readonly string|int|null $externalId = null, // scalar oneOf union
) {}
public static function rules(): array
{
return [
'name' => ['