PHP code example of crastlin / laravel-annotation

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

    

crastlin / laravel-annotation example snippets


 class IndexController
 {
   /**
    * @Route(url="login", method="post")
    */
   function index()
   {
     // todo
   }
 }

 class IndexController
 {
   /**
    * @PostMapping("login")
    */
   function index(){
     // todo
   }
 }

 Route::post('login', 'IndexController@index')->name('index.index');

 class IndexController
 {
    /**
     * @PostMapping("article/{cate}")
     */
    function index(){
    
    }
    
    /**
     * @PostMapping("article/{cate}/{id}/{page?}")
     */
    function detail(){
      // todo
    }
 }

 Route::post('article/{cate}', 'IndexController@index')->name('index.index');
 Route::post('article/{cate}/{id}/{page?}', 'IndexController@detail')->name('index.detail');

 /**
  * @Group(prefix="home", namespace="Home", middleware="user.check", as="User::", domain="xxx.com")
  */
 class IndexController
 {
   /** 
    * @Route(url=login, method=post|get)
    */
   function index()
   {
     // todo
   }
   
   /** 
    * @RequestMapping("reg")
    */
   function register()
   {
     // todo
   }
   
   /** 
    * @GetMapping
    */
   function userCenter()
   {
     // todo
   }
 }

 /**
  * @Group({"prefix":"home", "namespace":"Home", "middleware": "user.check", "as": "User::"})
  */
 class IndexController
 {
   /**
    * @Group({"limit": true})
    * @Route(url=login, method=post|get)
    */
   function index()
   {
     // todo
   }
   
   /**
    * @Group({"limit": true})
    * @RequestMapping("reg")
    */
   function register()
   {
     // todo
   }
   
   /** 
    * @GetMapping
    */
   function userCenter()
   {
     // todo
   }
 }

 Route::group(["preifx" => "home", "namespace" => "Home", "middleware" => "user.check", "as" => "User::"], function(){
   Route::group(["limit" => true], function(){
      Route::match(['GET', 'POST'], 'login', 'IndexController@index');
      Route::match(['GET', 'POST'], 'reg','IndexController@register');
   });
   Route::get('Index/userCenter','IndexController@userCenter');
 });
  // ...

return [
  'modules' => ['User', 'Admin'],
  'root_group' => [
     'User' => [
        ['prefix' => 'user', 'namespace' => 'User', 'middleware' => 'user.check', 'as' => 'User::'],
        // 更多分组
      ],
     'Admin' => [
        ['prefix' => 'admin', 'namespace' => 'Admin', 'middleware' => 'admin.check', 'as' => 'Admin::'],
        // 更多分组
     ],
   ],
];

 /**
  * @node (name="应用名称", parent="父节点", menu=0/1, auth=0/1/2, order=0, params="xx=yy&cc=ss", icon="xxx", remark="xxx", actions="defaultPage,xxx,yyyy")
  */

  /**
   * @node (name="节点名称", parent="父节点", menu=0/1, auth=0/1/2, order=0, params="xx=yy&cc=ss", icon="xxx", code="query", remark="xxx", ignore, delete)
   */
 

 class UserController
 {
   
    /**
    * @Node(name="用户管理", menu=1, auth=0)
    */
   function defaultPage()
   {
     // this method only for menu
   }

   /**
    * @Node(name="用户列表", menu=1)
    */
   function index()
   {
     // todo
   }
  
   /**
    * @Node(name="编辑用户", parent="index")
    */
   function setUserName()
   {
     // todo
   }

 }

 abstract class BaseController implements \LaravelAnnotationNodeInterface
 {
       /**
        * @Node(menu=1, auth=0)
        */
       function defaultPage()
       {
         // this method only for menu
       }
 }
 
 /**
  * @Node(name="用户管理", order=1)
  */
 class UserController extends BaseController
 {
     /**
      * @Node(name="用户列表", menu=1)
      */
     function index()
     {
       // todo
     }
    
     /**
      * @Node(name="编辑用户", parent="index")
      */
     function setUserName()
     {
       // todo
     }

 }

   /**
    * @Node(name="用户中心", menu=1, auth=0, order=1)
    */
   function defaultPage()
   {
     // this method only for menu
   }

 /**
  * @Node(name="动物园", order=1)
  */
 abstract class Animal extends BaseController
 {
   /**
    * @Node(name="主页", menu=1)
    */
   function index()
   {
     // todo
   }
   
   /**
    * @Node(name="观看时间", menu=1)
    */
   function schedule()
   {
     // todo
   }
 }
 
 /**
  * @Node(name="长颈鹿", actions=index, schedule)
  */
 class GiraffeController extends Animal
 {
 }
 
 /**
  * @Node(name="老虎")
  */
 class TigerController extends Animal
 {  
 }
 

 class Kernel extends \Illuminate\Foundation\Http\Kernel
 {
    protected $middleware = [
       // ...
       Crastlin\LaravelAnnotation\Middleware\InterceptorMiddleware::class,
    ];
 }

 class IndexController
 {
    /**
     * @RequestMapping("test")
     * @SyncLock(expire=30, suffix="$id")
     */
    function test()
    {
       //todo
    }
 }

 namespace App\Http\Middleware;
 use Crastlin\LaravelAnnotation\Facades\Injection;
 use \Illuminate\Http\Request;
 use App\Model\User;
 class AuthorizeCheck
 {
    function handle(Request $request)
    {
       $parameters = $request->getContent();
       // todo something
       // ...
       // 绑定数据到依赖类容器
       Injection::bind('parameters', $parameters);
       
       // 绑定用户
       try{
        $user = User::find($parameters['uid']);
        Injection::bind('user', $user);
       }catch (\Throwable $throwable){
         var_dump("用户不存在");
       }
    }
 }


// 基类配置注入方法

namespace Illuminate\Routing\Controller;
use Crastlin\LaravelAnnotation\Facades\Injection;
use Crastlin\LaravelAnnotation\Annotation\Annotations\Inject;
use Crastlin\LaravelAnnotation\Annotation\Annotations\PostMapping;

abstract class BaseController extends Controller
{
   // 通用注入属性方法
    function setProperty(string $name, $value)
    {
        if (property_exists($this, $name))
            $this->{$name} = $value;
    }
    
    // 重写callAction
    public function callAction($method, $parameters)
    {
        $input = Input::toArray();
        // 解析当前控制器对象属性注解,并自动注入
        Injection::injectWithObject($this);
        // call controller action
        return call_user_func_array([$this, $method], $parameters);
    }
}


class IndexController extends BaseController
{

  // 在以下属性增加Inject注解
  /**
   * @var array $parameters
   * @Inject 
   */
  protected $parameters;

  /**
   * @PostMapping
   */
  function index()
  {
     // 当前访问index方法时,可以直接访问注入的属性
     var_dump($this->parameters);
     // 使用take方法直接取值
     $user = Injection::take('user');
     var_dump($user);
     // 使用exists判断容器是否绑定对象
     $exists = Injection::exists('user');
     var_dump($exists);
  }
}


class IndexController extends BaseController
{

  // 在以下属性增加Inject注解
  /**
   * @var array $data
   * @Inject(name="parameters")
   */
  protected $data;

  /**
   * @PostMapping
   */
  function index()
  {
     // 当前访问index方法时,可以直接访问注入的属性
     var_dump($this->parameters);
  }
}



// 在中间件中绑定带前缀的数据或对象
 namespace App\Http\Middleware;
 use Crastlin\LaravelAnnotation\Facades\Injection;
 use \Illuminate\Http\Request;
 class AuthorizeCheck
 {
    function handle(Request $request)
    {
       $parameters = $request->getContent();
       // todo something
       // ...
       // 绑定数据到依赖类容器
       Injection::bind('common.parameters', $parameters);
    }
 }


class IndexController extends BaseController
{

  // 在以下属性增加Inject注解
  /**
   * @var array $data
   * @Inject(name="common.parameters")
   * 或者配置prefix
   * @Inject(name="parameters", prefix="common")
   */
  protected $data;

  /**
   * @PostMapping
   */
  function index()
  {
     // 当前访问index方法时,可以直接访问注入的属性
     var_dump($this->parameters);
  }
}


namespace App\Service;
use Crastlin\LaravelAnnotation\Utils\Traits\SingletonTrait;

class BusinessService
{
  use SingletonTrait;
  /**
   * @var array $data
   * @Inject(name="common.parameters")
   */
  protected $data;
  
  function profile()
  {
     var_dump($this->data);
  }
  
}

namespace App\Http\Controllers\Api;
use App\Service\BusinessService;

class IndexController extends BaseController
{
 
  /**
   * @PostMapping
   */
  function index()
  {
     $service = BusinessService::singleton();
     $service->profile();
  }
}

namespace App\Service;
use Crastlin\LaravelAnnotation\Utils\Traits\SingletonTrait;

class BusinessService
{
  use SingletonTrait;
  /**
   * @var array $data
   * @Inject(name="common.parameters")
   */
  protected $data;
  
  
  // 使用set + 属性名(小驼峰命名规则)
  function setData(?array $data)
  {
    // todo something
    $this->data = $data;
  }
  
  function profile()
  {
     var_dump($this->data);
  }
  
}

namespace App\Service;
use Crastlin\LaravelAnnotation\Utils\Traits\SingletonTrait;
use App\Model\User;

class BusinessService
{
  use SingletonTrait;
  /**
   * @var User $user
   */
  protected $user;
  
  
  /**
   * @Inject(name="service.user")
   */
  function takeUser(?User $user)
  { 
    $this->user = $user;
  }
  
  function getUser()
  {
     var_dump($this->user);
  }
  
}


class Kernel extends \Symfony\Component\HttpKernel\HttpKernel
{
  
  protected $middleware = [
     // .... 
     // 引入拦截器中间件
     \Crastlin\LaravelAnnotation\Middleware\InterceptorMiddleware::class,
  ];
}

namespace App\Http\Controllers\Api;
use App\Service\BusinessService;
use Crastlin\LaravelAnnotation\Annotation\Annotations\Validation;

class IndexController extends BaseController
{
 
  /**
   * @PostMapping
   * @Validation(field="username", rule="

namespace App\Http\Controllers\Api;
use App\Service\BusinessService;
use Crastlin\LaravelAnnotation\Annotation\Annotations\Validation;

class IndexController extends BaseController
{
 
  /**
   * @PostMapping
   * @Validation(field="mobile", rule="

namespace App\Http\Controllers\Api;
use App\Service\BusinessService;
use Crastlin\LaravelAnnotation\Annotation\Annotations\Validation;

class IndexController extends BaseController
{
 
  /**
   * @PostMapping
   * @Validation(class="Mobile")
   */
  function index()
  { 
  }
}

namespace App\Validator;
use App\Service\BusinessService;
use Crastlin\LaravelAnnotation\Utils\Validate;

class Mobile extends Validate
{
 
  protected $rules = [
        'mobile' => '      'code.digits_between' => ':attribute必须为3到6位',
    ],
        $attributes = [
        'mobile' => '手机号',
        'code' => '验证码',
    ];
    
}

namespace App\Http\Controllers\Api;
use App\Service\BusinessService;
use Crastlin\LaravelAnnotation\Annotation\Annotations\Validation;

class IndexController extends BaseController
{
 
  /**
   * @PostMapping
   * @Validation\Required(username)
   * @Validation\AlphaNum(username)
   * @Validation\Regex(field="username", rule="~^\w{6,20}$~") 
   */
  function index()
  { 
  }
}
`shell script
php artisan annotation:config
 
`shell script
php artisan annotation:route {module?}
`shell script
php artisan annotation:node {module?}