PHP code example of ayd / api-response-hyperf

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

    

ayd / api-response-hyperf example snippets


use Hyperf\HttpServer\Contract\ResponseInterface;
use Ayd\ApiResponseHyperf\ApiResponse;

class UserController
{
    public function __construct(
        private ApiResponse $response,
        private ResponseInterface $hyperfResponse,
    ) {
        $this->response->setResponse($this->hyperfResponse);
    }

    public function index()
    {
        return $this->response->success($users);
    }

    public function store()
    {
        // validation...

        return $this->response->created($user, 'User created');
    }

    public function show($id)
    {
        $user = User::find($id);

        if (!$user) {
            return $this->response->notFound('User not found');
        }

        return $this->response->success($user);
    }
}

use Ayd\ApiResponseHyperf\ApiResponseTrait;

class UserController
{
    use ApiResponseTrait;

    public function index()
    {
        return $this->success(User::all());
    }

    public function store()
    {
        // validation...

        return $this->created($user, 'User created');
    }

    public function destroy($id)
    {
        User::findOrFail($id)->delete();

        return $this->noContent();
    }
}

public function show($id)
{
    $user = User::find($id);

    if (!$user) {
        return $this->notFound('User not found');
    }

    return $this->success($user);
}

use Ayd\ApiResponseBase\Contracts\AbilitiesResolver;

return [
    'dependencies' => [
        AbilitiesResolver::class => MyAbilitiesResolver::class,
    ],
];