PHP code example of lanser / laravel-openapi-generator
1. Go to this page and download the library: Download lanser/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/ */
lanser / laravel-openapi-generator example snippets
composer update
'version' => env('APP_VERSION', '1.0.0'),
use Lanser\LaravelApiGenerator\Request\RequestData;
class UserRequest extends RequestData
{
public function __construct(
public string $name,
public string $email
)
{
}
public static function rules(ValidationContext $context): array
{
return [
'name' => ['
use Lanser\LaravelApiGenerator\Response\ResponseData;
class UserResponse extends ResponseData
{
public function __construct(
public string $name,
public string $email
)
{
}
}
namespace App\Http\Controllers;
use App\Http\Requests\UserRequest;
use App\Response\UserResponse;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\User;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
/**
* @param UserRequest $request
* @return UserResponse
*/
public function show(UserRequest $request): UserResponse
{
$user = User::query()->first();
return UserResponse::from($user);
}
}
Route::group(['as' => 'user'], function () {
Route::post('/', [Controller::class, 'show']);
});
Route::group(['as' => 'user'], function () {
Route::post('/', [Controller::class, 'show']);
})->middleware('auth:sanctum');
use Lanser\LaravelApiGenerator\OpenApi\Attributes\ErrorMessage;
#[ErrorMessage(["User not found", "User is not active"])]
public function show(UserRequest $request): UserResponse
{
$user = User::query()->first();
return UserResponse::from($user);
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.