cryode/annotaction

一个由注解驱动的动作的 Laravel 扩展包。

0.3.0 2018-09-10 19:54 UTC

This package is not auto-updated.

Last update: 2024-09-19 14:15:47 UTC


README

注解动作控制器

注解允许您创建和使用单动作控制器(动作),这些控制器使用注解来定义其对应的路由。

<?php

use Cryode\Annotaction\Annotation\Route;

/**
 * @Route("/blog/post/{postId}", middleware="web", name="blog.post.view")
 */
final class ViewBlogPost
{
    public function __invoke($postId)
    {
        // ...
    }
}

优势

  • 单一目的控制器(动作)有助于保持您的代码整洁、精简,并且有助于提高性能。
  • 动作文件中的路由定义将相关关注点集中在一起 - 无需猜测控制器具有的 URI
  • 使用路由缓存时没有性能损失

安装

要求:PHP 7.1+

Annotaction 通过 Composer 安装。

composer require cryode/annotaction

Laravel 的包发现应自动在安装后加载服务提供程序。如果您已禁用自动发现,请手动将服务提供程序添加到您的应用程序中

\Cryode\Annotaction\AnnotactionServiceProvider::class

对于 PhpStorm 用户,PHP Annotations 插件 非常有用。

配置

运行发布命令将配置文件添加到您的应用程序中(可选,如果您想更改默认值)

php artisan vendor:publish --provider="Cryode\Annotaction\AnnotactionServiceProvider" --tag="config"

配置选项

  • action_dir | string | 默认: /app/Http/Actions
    您的动作所在并生成的目录。

  • default_middleware | array[string] | 默认: ['web']
    通过 Artisan 生成动作时的默认中间件注解值。

  • api_middleware | array[string] | 默认: ['api']
    通过 Artisan 使用 --api 标志生成动作时的默认中间件注解值。

  • generate_strict_types | bool | 默认: true
    标志用于在生成的动作文件中放置 define(strict_types=1)

通过 Artisan 创建动作

Annotaction 随带 Artisan 命令 make:action,用于帮助生成动作文件。

简单用法:$ php artisan make:action MyActionName

选项

--resource                 Flag to create collection of Resource Actions
--api                      Flag to specify Action(s) as meant for an API
--path[=PATH]              The route URI/path for the Action [default: "/"]
--name[=NAME]              The route Name for the Action
--method[=METHOD]          The HTTP Method for the Action
--middleware[=MIDDLEWARE]  The Route Middleware for the Action; default in config
--force                    Force recreation of Action if it already exists

--api 标志有两个作用

  1. 如果没有指定 --middleware 值,将使用 api_middleware 配置值。
  2. 如果通过 --resource 创建资源动作,它将跳过创建和编辑,因为 API 没有用户界面。