deva7mad/lumen-annotations

Laravel Lumen 路由和事件绑定注解生成器

1.1 2018-10-06 00:45 UTC

This package is auto-updated.

Last update: 2024-09-12 11:00:24 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

此包使 Laravel Lumen 能够使用注解来定义路由和事件绑定。

安装

Lumen Annotations 以 composer 包的形式分发。因此,您首先需要运行以下命令

composer require deva7mad/lumen-annotations

之后,将此包中的 config/annotations.php 复制到您的配置目录。

最后:将这些行添加到 bootstrap/app.php 文件中

$app->configure('annotations');
$app->register(DevA7mad\Annotations\AnnotationsServiceProvider::class);
包含生成的路由

运行 php artisan route:scan 后(注意:请参阅以下示例),您需要将生成的 generated.php 文件包含在 bootstrap/app.php 文件中

require __DIR__.'/../routes/generated.php';

使用方法

通过使用注解,您可以直接在控制器类中定义路由,直接在事件处理器中定义事件绑定(请参阅注解的使用示例)。

类注解

用于路由

用于事件

方法注解

用于路由

命令

在通过注解定义了路由和事件绑定后,您需要运行扫描命令

  • 使用 php artisan route:scan 来注册所有路由。
  • 使用 php artisan route:clear 来清除已注册的路由。
  • 使用 php artisan event:scan 来注册所有事件绑定。
  • 使用 php artisan event:clear 来清除已注册的事件。

示例

示例 #1
<?php

namespace App\Http\Controllers;

use DevA7mad\Annotations\Annotations as Route;

/**
 * Class annotation for UserController (belongs to all class methods).
 *
 * @Route\Controller(prefix="admin")
 */
class UserController
{
    /**
     * Method annotations for showProfile($id) method.
     * @param $id
     * @Route\Get("profiles/{id}", as="profiles.show")
     * @return mixed
     */
    public function showProfile($id)
    {
        return $id;
    }

}
示例 #2
<?php

namespace App\Http\Controllers;

use DevA7mad\Annotations\Annotations as Route;

/**
 * Class annotations for resource controller CommentController (belongs to all class methods).
 *
 * @Route\Controller
 * @Route\Resource("comments", only={"create", "index", "show"})
 * @Route\Middleware("auth")
 */
class CommentController
{
    ...
}
示例 #3
<?php

namespace App\Handlers\Events;

use DevA7mad\Annotations\Annotations\Hears;

/**
 * Annotation for event binding.
 *
 * @Hears("UserWasRegistered")
 */
class SendWelcomeMail
{
    ...
}

支持

错误和功能请求在 GitHub 上跟踪。

作者

许可证

此包在 MIT 许可证 下发布。

在 Beerpay 上支持

嘿,兄弟!帮助我几个 🍻!

Beerpay Beerpay