PHP code example of sorry510 / annotation

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

    

sorry510 / annotation example snippets


php artisan vendor:publish --tag=annotation



namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    ...

    protected $middlewareGroups = [
        'api' => [
            'queryAuth',
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
            'annotation', // 注解中间件
        ],
    ];

    ...
}


 
namespace App\validate;

use Sorry510\Annotations\Validate\Validate;

class FormValidate extends Validate
{
    // 验证规则
    protected function rule(): array
    {
        return [
            'name' => '为空',
        'max' => ':attribute长度最多为max',
        'unique' => ':attribute必须唯一',
    ];

    // 自定义字段名称,提示的时候用到
    protected $custom = [
        'name' => '角色名称',
        'describe' => '角色描述',
    ];

    // 场景
    protected $scene = [
        "add" => ["name", "describe"],
        "edit" => ["name", "describe"],
    ];
}

use App\Models\UnitModel;

class UnitLogic2
{
    private $model;

    public function __construct(UnitModel $model)
    {
        $this->model = $model;
    }
}

use App\Models\UnitModel;

class UnitLogic3
{
    private $model;
	
	private $other;

    public function __construct(UnitModel $model, $other = null)
    {
        $this->model = $model;
		$this->other = $other;
    }
}