PHP code example of treblle / api-responses

1. Go to this page and download the library: Download treblle/api-responses 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/ */

    

treblle / api-responses example snippets


return [
    'headers' => [
        'default' => [
            'Content-Type' => 'application/vnd.api+json',
        ],
        'error' => [
            'Content-Type' => 'application/problem+json',
        ],
    ],
];

final class ShowController
{
    public function __invoke(Request $request, User $user): Responsable
    {
        return new ModelResponse(
            data: new UserResource(
                resource: $user,
            ),
        );
    }
}

final class IndexController
{
    public function __invoke(Request $request): Responsable
    {
        return new CollectionResponse(
            data: UserResource::collection(
                resource: User::query()->get(),
            ),
        );
    }
}

final class Handler extends ExceptionHandler
{
    public function register(): void
    {
        $this->renderable(fn (ModelNotFoundException $exception, Request $request) => new ErrorResponse(
            data: new ApiError(
                title: 'Not Found',
                detail: $exception->getMessage(),
                instance: $request->path(),
                code: ErrorCode::NOT_FOUND->value,
                link: 'https://docs.domain.com/errors/not-found',
            ),
            status: Status::NOT_FOUND,
        ));
    }
}

final class StoreController
{
    public function __invoke(StoreRequest $request): Responsable
    {
        dispatch(new RegisterProvider($request->payload()));
        
        return new MessageResponse(
            data: 'We have accepted your request, and are processing this action.',
            status: Status::ACCEPTED,
        )
    }
}

final class LoginController
{
    public function __invoke(Request $request): Responsable
    {
        return new \Treblle\ApiResponses\Responses\ExpandedResponse(
            message: __('auth.login'),
            data: [
                'type' => 'login',
                'attributes' => [
                    'mfa' => __('auth.mfa_
bash
php artisan vendor:publish --tag=api-config