PHP code example of baoziyoo / hyperf-dto

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

    

baoziyoo / hyperf-dto example snippets




declare(strict_types=1);

namespace Baoziyoo\Hyperf\Example\DTO;

class Address
{
    public string $street;

    public float $float;
    
    public int $int;
    
    /** @var array<int,string> */
    public array $array;
    
    public LoginTokenTypeEnum $loginTokenTypeEnum;
    
    public ?City $city = null;
}

---

class City
{
    public string $name;
}

---

enum LoginTokenTypeEnum: string
{
    case jwt = 'jwt';
    
    case password = 'password';
}

use Baoziyoo\Hyperf\DTO\Annotation\Contracts\RequestBody;
use Baoziyoo\Hyperf\DTO\Annotation\Contracts\RequestQuery;
use Baoziyoo\Hyperf\DTO\Annotation\Contracts\RequestFormData;


// 获取Body参数
public function add(#[RequestBody] Address $request){}

// 获取GET参数
public function add(#[RequestQuery] Address $request){}

// 获取表单请求
public function fromData(#[RequestFormData] Address $formData){}

// 获取Body参数和GET参数
public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query){}

class DemoController extends AbstractController
{
    public function index(#[RequestQuery] DemoQuery $request): Contact
    {
        $contact = new Contact();
        $contact->name = $request->name;
        var_dump($request);
        return $contact;
    }

    public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query)
    {
        var_dump($query);
        return json_encode($request, JSON_UNESCAPED_UNICODE);
    }

    public function fromData(#[RequestFormData] DemoFormData $formData): bool
    {
        $file = $this->request->file('photo');
        var_dump($file);
        var_dump($formData);
        return true;
    }
}