PHP code example of lazyskills / think-annotation

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

    

lazyskills / think-annotation example snippets


    
    // +----------------------------------------------------------------------
    // | 控制台配置
    // +----------------------------------------------------------------------
    return [
        // 指令定义
        'commands' => [
            \think\annotation\command\Annotation::class,
            \think\annotation\command\Handler::class
        ],
    ];
    

  php think make:annotation User
  

  
  declare (strict_types = 1);
  
  namespace app\annotation;
  
  use Doctrine\Common\Annotations\Annotation;
  
  /**
   * class User
   * @package app\annotation
   * @Annotation
   * @Target({"METHOD","CLASS"}) # 不需要进行类注解去掉"CLASS",不需要方法注解去掉"METHOD"
   */
  class User extends Annotation
  {
      // TODO 完成你对注解字段的定义
  }
  

  php think make:handler User
  

  
  declare (strict_types = 1);
  
  namespace app\annotation\handler;
  
  use Doctrine\Common\Annotations\Annotation;
  use think\annotation\handler\Handler;
  
  class User extends Handler
  {
      public function cls(\ReflectionClass $refClass, Annotation $annotation, \think\Route &$route)
      {
          // TODO: 完成类注解的操作
      }
  
      public function func(\ReflectionMethod $refMethod, Annotation $annotation, \think\route\RuleItem &$rule)
      {
          // TODO: 完成方法注解的操作
      }
  }
  

   return [
       'inject' => [
           'enable'     => true,
           'namespaces' => [],
       ],
       'route'  => [
           'enable'      => true,
           'controllers' => [],
       ],
       'ignore' => [],
       'management' => true,
       'custom' => [
           # 格式:注解类 => 注解操作类
           \app\annotation\User::class => \app\annotation\handler\User::class, # 这里写上你的注解
       ]
   ];
  


namespace app\controller;

use think\annotation\Inject;
use think\annotation\Route;
use think\annotation\route\Group;
use think\annotation\route\Middleware;
use think\annotation\route\Resource;
use think\Cache;
use think\middleware\SessionInit;

/**
 * Class IndexController
 * @package app\controller
 * @Group("bb")
 * @Resource("aa")
 * @Middleware({SessionInit::class})
 */
class IndexController
{

    /**
     * @Inject()
     * @var Cache
     */
    protected $cache;

    public function index()
    {
        //...
    }

    /**
     * @Route("xx")
     */
    public function xx()
    {
        //...
    }