PHP code example of wayhood / hyperf-dto

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

    

wayhood / hyperf-dto example snippets


public function add(#[RequestBody] DemoBodyRequest $request){}

public function add(#[RequestQuery] DemoQuery $request){}

public function fromData(#[RequestFormData] DemoFormData $formData){}

#[ApiFormData(name: 'photo', type: 'file')]

public function add(#[RequestBody] DemoBodyRequest $request, #[RequestQuery] DemoQuery $query){}

#[Controller(prefix: '/demo')]
#[Api(tags: 'demo管理', position: 1)]
class DemoController extends AbstractController
{
    #[ApiOperation(summary: '查询')]
    #[PostMapping(path: 'index')]
    public function index(#[RequestQuery] #[Valid] DemoQuery $request): Contact
    {
        $contact = new Contact();
        $contact->name = $request->name;
        var_dump($request);
        return $contact;
    }

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

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

    #[GetMapping(path: 'find/{id}/and/{in}')]
    public function find(int $id, float $in): array
    {
        return ['$id' => $id, '$in' => $in];
    }

}


public function index(#[RequestQuery] #[Valid] DemoQuery $request){}

class DemoQuery
{
    public string $name;

    #[Required]
    #[Integer]
    #[Between(1,5)]
    public int $num;
}

#[Attribute(Attribute::TARGET_PROPERTY)]
class Image extends BaseValidation
{
    protected $rule = 'image';
}

    /**
     * 需要绝对路径.
     * @var \App\DTO\Address[]
     */
    #[ApiModelProperty('地址')]
    public array $addressArr;