1. Go to this page and download the library: Download buexplain/arg 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/ */
buexplain / arg example snippets
namespace App\Arg;
use Arg\Attr\ValidationAttr;
use Arg\BaseArgForHyperf;
/**
* 注册一个用户
*/
class RegisterArg extends BaseArgForHyperf
{
/**
* @var string 账号
*/
#[ValidationAttr('lic string $verification_code;
}
declare(strict_types=1);
namespace App\Controller;
use App\Arg\RegisterArg;
use App\Services\RegisterService;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class IndexController extends AbstractController
{
/**
* 注册用户
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function index(): array
{
//初始化参数类
$registerArg = new RegisterArg($this->request->all());
//校验数据
$msgBag = $registerArg->validate();
if ($msgBag->isNotEmpty()) {
//校验失败,返回错误信息
return [
'code' => 1,
'message' => $msgBag->first(),
];
}
//调用service层的注册逻辑,执行注册动作,并返回注册结果
return [
'code' => 0,
'data' => RegisterService::create($registerArg),
'message' => '注册成功',
];
}
}
declare(strict_types=1);
namespace App\Services;
use App\Arg\RegisterArg;
class RegisterService
{
/**
* 注册用户
* @param RegisterArg $arg
* @return array
*/
public static function create(RegisterArg $arg): array
{
//$arg因为是一个类,所以里面有什么属性,编辑器可以识别的一清二楚。
//开始注册逻辑,这里假设注册成功后返回账号信息
return ['account' => $arg->account];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.