PHP code example of martinpham / laravel-openapi-generator

1. Go to this page and download the library: Download martinpham/laravel-openapi-generator 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/ */

    

martinpham / laravel-openapi-generator example snippets


  
namespace App\Data;  
  
use App\Models\User;  
use Spatie\LaravelData\Data;  
  
class GreetingData extends Data  
{  
    public function __construct(  
        public User         $user,  
        public string       $message  
    ){}  
}  


namespace App\Http\Controllers\Api;  

use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Group;
use App\Data\GreetingData;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Collection;
use App\Models\User;

#[Group('/play', as: 'play')]
class PlaygroundApiController extends Controller
{  
    #[Get('/who-is/{user}', name: ".whois")]
    public function hello(User $user): User
    {
        return $user;
    }

    
    #[Get('/user', name: ".user")]
    /** @return Collection<int, User> */
    public function hello(): Collection
    {
        return User::all();
    }

    #[Get('/hello/{user}', name: ".hello")]
    public function hello(User $user): GreetingData
    {
        return GreetingData::from($user, "hello ${user->name}");
    }
}