elemenx/lumen-advanced-route

支持嵌套分组与资源。

0.2.0 2021-01-27 06:20 UTC

This package is auto-updated.

Last update: 2024-09-27 14:35:36 UTC


README

受fremail/lumen-nested-route-groups启发,增加了对Lumen资源路由的支持。

变更日志

  • v.0.1.1 删除无用代码。
  • v.0.1.0 添加对Lumen 5.5和资源支持。

安装方法(步骤)

1. 使用Composer安装

composer require "elemenx/lumen-advanced-route:~0.1"

2. 修改bootstrap/app.php中的必需更改

在bootstrap/app.php中将Lumen应用程序类的初始化更改为Lumen嵌套路由分组应用程序类的初始化。

之前

$app = new Laravel\Lumen\Application(
    realpath(__DIR__.'/../')
);

之后

$app = new ElemenX\AdvancedRoute\Application(
    realpath(__DIR__.'/../')
);

Any()和Match()以及resource()方法

你喜欢Laravel中的any()match()方法吗?我非常喜欢它们!这就是为什么我在Lumen中添加了支持它们。语法与Laravel相同

$app->match($methods, $uri, $action);

其中$methods - 方法数组。例如:['get', 'post', 'delete']$uri$action与其它方法相同

$app->any($uri, $action);

在这里,$uri$method与其它方法如$app->get(...)等相同。

此库的使用示例

这是routes/web.php的示例

$app->group(['middleware' => 'auth'], function () use ($app) {

    $app->get('test', function () {
        echo "Hello world!";
    });

    $app->resource('user', 'UserController', ['only' => ['show', 'store', 'destroy']]);

    /**
     * only admins
     */
    $app->group(['middleware' => 'admin'], function () use ($app) {

        $app->group(['prefix' => 'admin'], function () use ($app) {
            $app->get('/', 'AdminController@index');
        });

    });
    
    /**
     * $app->any and $app->match available from v1.1.0
     */
    $app->any('/', function () use ($app) {
        echo "Hey! I don't care it's POST, GET, PATCH or another method. I'll answer on any of them :)";
    });
    
    $app->match(['PATCH', 'PUT', 'DELETE'], '/old/', function () use ($app) {
        echo "This is an old part of our site without supporting REST. Please use only GET and POST here.";
    });

});