PHP code example of littler / annotation

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

    

littler / annotation example snippets




namespace app\controller;

use littler\Inject;
use littler\Route;
use littler\route\Group;
use littler\route\Middleware;
use littler\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()
    {
        //...
    }

}


  
  // +----------------------------------------------------------------------
  // | 控制台配置
  // +----------------------------------------------------------------------
  return [
      // 指令定义
      'commands' => [
          \littler\command\Annotation::class,
          \littler\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 littler\handler\Handler;

  class User extends Handler
  {
      public function cls(\ReflectionClass $refClass, Annotation $annotation, \think\Route\RuleGroup &$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' => [
         'enable'      => true,
         # 格式:注解类 => 注解操作类
         'namespaces' => [
             \app\annotation\User::class => \app\annotation\handler\User::class, # 这里写上你的注解
         ],
      ],
  ];