proai / lumen-annotations
Laravel Lumen 路由和事件绑定注解
1.0.2
2015-12-30 01:13 UTC
Requires
- php: >=5.5.9
- doctrine/annotations: ~1.2
This package is not auto-updated.
Last update: 2024-09-14 18:47:37 UTC
README
此包为 Laravel Lumen 提供了注解功能,用于定义路由和事件绑定。
安装
Lumen Annotations 以 composer 包的形式提供。因此,您首先需要将包添加到您的 composer.json
文件中
"proai/lumen-annotations": "~1.0"
然后,您必须运行 composer update
来安装包。一旦完成,您需要将服务提供者在 bootstrap/app.php
中添加
$app->register(ProAI\Annotations\AnnotationsServiceProvider::class);
将此包中的 config/annotations.php
复制到您的配置目录,以使用自定义配置文件。
包含生成的路由
一旦您运行了 php artisan route:scan
(见下文),您需要将生成的 routes.php
文件包含在您的 bootstrap/app.php
文件中
require __DIR__.'/../storage/framework/routes.php';
包含生成的事件绑定
在执行 php artisan event:scan
(见下文)之后,您需要在 config/app.php
的 providers 数组中添加服务提供者
'ProAI\Annotations\EventServiceProvider'
使用方法
通过使用注解,您可以直接在控制器类中定义路由,并在事件处理器中直接定义事件绑定(请参阅注解的使用示例)。
类注解
路由注解
事件注解
方法注解
路由注解
命令
通过注解定义路由和事件绑定后,您必须运行扫描命令
- 使用
php artisan route:scan
注册所有路由。 - 使用
php artisan route:clear
清除已注册的路由。 - 使用
php artisan event:scan
注册所有事件绑定。 - 使用
php artisan event:clear
清除已注册的事件。
示例
示例 #1
<?php namespace App\Http\Controllers; use ProAI\Annotations\Annotations as Route; /** * Class annotation for UserController (belongs to all class methods). * * @Route\Controller(prefix="admin") */ class UserController { /** * Method annotations for showProfile() method. * * @Route\Get("/profiles/{id}", as="profiles.show") * @Route\Middleware("auth") */ public function showProfile() { return view('profile'); } }
示例 #2
<?php namespace App\Http\Controllers; use ProAI\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 ProAI\Annotations\Annotations\Hears; /** * Annotation for event binding. * * @Hears("UserWasRegistered") */ class SendWelcomeMail { ... }
支持
错误和功能请求在 GitHub 上跟踪。
许可证
此包在 MIT 许可证 下发布。