PHP code example of caterpillar / hyperf-annotation-parse-body
1. Go to this page and download the library: Download caterpillar/hyperf-annotation-parse-body 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/ */
caterpillar / hyperf-annotation-parse-body example snippets
declare(strict_types=1);
namespace xxx;
use Caterpillar\HyperfAnnotationParseBody\Annotation\ParseBody;
use Hyperf\HttpServer\Annotation\GetMapping;
// 举例一个控制器
class TestController {
#[GetMapping(path: '/testAnnotationByFunction')]
#[ParseBody]
public function testAnnotationFunction(TestEntity $testEntity)
{
return [
'testEntity' => $testEntity->getName()
]
}
}
class TestEntity {
private string $name;
private int $age;
private string $birth_day;
// ... getter/setter
}
declare(strict_types=1);
namespace xxx;
use Caterpillar\HyperfAnnotationParseBody\Annotation\ParseBody;
use Hyperf\HttpServer\Annotation\GetMapping;
// 举例一个控制器
class TestController {
#[GetMapping(path: '/testAnnotationByFunction')]
#[ParseBody]
public function testAnnotationFunction(?string $name, ?int $age, ?$birth_day)
{
return [
'name' => $name,
'age' => $age,
'birth_day' => $birth_day
]
}
}
declare(strict_types=1);
namespace xxx;
use Caterpillar\HyperfAnnotationParseBody\Annotation\ParseBody;
use Hyperf\HttpServer\Annotation\GetMapping;
// 类注解则本类均可生效
#[ParseBody]
class TestController {
#[GetMapping(path: '/testAnnotationByFunction')]
#[ParseBody]
public function testAnnotationFunction(UserEntity $user, ?int $id)
{
return [
'user' => $user->getProperty()->getCar(),
'age' => $age,
'birth_day' => $birth_day
]
}
}
class UserEntity {
private int $id;
private Property $property;
// getter/setter
}
class Property {
private bool $has_car;
private bool $has_house;
// getter/setter
}