bfg/route 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bfg/route

Composer 安装命令:

composer require bfg/route

包简介

Auto register routes using PHP attributes

README 文档

README

Latest Version on Packagist

This package provides annotations to automatically register routes. Here's a quick example:

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Resource;

class MyController
{
    #[Get('my-route')]
    public function myMethod()
    {

    }
}

#[Resource('my_resource')]
class MyResourceController
{
    ...
}

This attribute will automatically register this route:

Route::get('my-route', [MyController::class, 'myMethod']);

Installation

You can install the package via composer:

composer require bfg/route

Usage

In your RouteServiceProvider, delete where your controllers are located and he will do the rest for you:

public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {

        Route::find(
            // Path for search attributes,
            // you can use class namespaces,
            // directories and file paths
            __DIR__ . '/../Http/Controllers',
            
            // Here you can transfer the parent
            // instance of the route from which
            // the nesting will be created.
            Route::middleware('web')
        );
    });
}

The package provides several annotations that should be put on controller classes and methods. These annotations will be used to automatically register routes

Adding a GET route

use Bfg\Route\Attributes\Get;

class MyController
{
    #[Get('my-route')]
    public function myMethod()
    {

    }
}

This attribute will automatically register this route:

Route::get('my-route', [MyController::class, 'myMethod']);

Using other HTTP verbs

We have left no HTTP verb behind. You can use these attributes on controller methods.

#[Bfg\Route\Attributes\Post('my-uri')]
#[Bfg\Route\Attributes\Put('my-uri')]
#[Bfg\Route\Attributes\Patch('my-uri')]
#[Bfg\Route\Attributes\Delete('my-uri')]
#[Bfg\Route\Attributes\Options('my-uri')]

Specify a route name

All HTTP verb attributes accept a parameter named name that accepts a route name.

use Bfg\Route\Attributes\Get;

class MyController
{
    #[Get('my-route', name: "my-route-name")]
    public function myMethod()
    {

    }
}

This attribute will automatically register this route:

Route::get('my-route', [MyController::class, 'myMethod'])->name('my-route-name');

Adding middleware

All HTTP verb attributes accept a parameter named middleware that accepts a middleware class or an array of middleware classes.

use Bfg\Route\Attributes\Get;

class MyController
{
    #[Get('my-route', middleware: MyMiddleware::class)]
    public function myMethod()
    {

    }
}

This annotation will automatically register this route:

Route::get('my-route', [MyController::class, 'myMethod'])->middleware(MyMiddleware::class);

To apply middleware on all methods of a class you can use the Middleware attribute. You can mix this with applying attribute on a method.

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Middleware;

#[Middleware(MyMiddleware::class)]
class MyController
{
    #[Get('my-route')]
    public function firstMethod()
    {
    }

    #[Get('my-other-route', middleware: MyOtherMiddleware::class)]
    public function secondMethod()
    {
    }
}

These annotations will automatically register these routes:

Route::get('my-route', [MyController::class, 'firstMethod'])->middleware(MyMiddleware::class);
Route::get('my-other-route', [MyController::class, 'secondMethod'])->middleware([MyMiddleware::class, MyOtherMiddleware]);

Specifying a prefix

You can use the Prefix annotation on a class to prefix the routes of all methods of that class.

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Post;
use Bfg\Route\Attributes\Prefix;

#[Prefix('my-prefix')]
class MyController
{
    #[Get('my-get-route')]
    public function myGetMethod()
    {
    }

    #[Post('my-post-route')]
    public function myPostMethod()
    {
    }
}

These annotations will automatically register these routes:

Route::get('my-prefix/my-get-route', [MyController::class, 'myGetMethod']);
Route::post('my-prefix/my-post-route', [MyController::class, 'myPostMethod']);

Specifying a domain

You can use the Domain annotation on a class to prefix the routes of all methods of that class.

use Bfg\Route\Attributes\Get;
use Bfg\Route\Attributes\Post;
use Bfg\Route\Attributes\Domain;

#[Domain('my-subdomain.localhost')]
class MyController
{
    #[Get('my-get-route')]
    public function myGetMethod()
    {
    }

    #[Post('my-post-route')]
    public function myPostMethod()
    {
    }
}

These annotations will automatically register these routes:

Route::get('my-get-route', [MyController::class, 'myGetMethod'])->domain('my-subdomain.localhost');
Route::post('my-post-route', [MyController::class, 'myPostMethod'])->domain('my-subdomain.localhost');

Deployment

As stated in the documentation which you can see here. After you cache your routes ...

php artisan route:cache

... scanning of your classes will be disabled.

Testing

cd vendor/bfg/route
composer install
composer test

Inspired by

I took this package into the service and reworked it a little, added a couple of functions, caching and added the ability to extend it a little, I plan to support a more advanced API as far as possible.

License

The MIT License (MIT). Please see License File for more information.

bfg/route 适用场景与选型建议

bfg/route 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 159 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 01 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「route」 「attributes」 「laravel」 「spatie」 「bfg」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 bfg/route 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bfg/route 我们能提供哪些服务?
定制开发 / 二次开发

基于 bfg/route 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 159
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-26