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

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

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

bfg/route

最新稳定版本:1.2.5

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.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固