five-say/laravel-route-group

There is no license information available for the latest version (v2.0.0) of this package.

Object-oriented route group.use like this: $route->get(...)->as(...)->uses(...)->before(...);

v2.0.0 2014-09-29 15:04 UTC

This package is not auto-updated.

Last update: 2024-11-05 01:58:07 UTC


README

对象化分组路由辅助工具

使用前的准备

在 composer.json 文件中申明依赖:

"five-say/laravel-route-group": "1.*"

/app/config/app.php 中设置“服务提供者”与“别名”

'providers' => array(
    ...
    'FiveSay\LaravelRouteGroup\ServiceProvider',
),
'aliases' => array(
    ...
    'RouteGroup' => 'FiveSay\LaravelRouteGroup\Facade',
),

使用方法

RouteGroup::make()->controller('AdminController')->go(function ($route) {
    $route->get('/')->as('admin')->uses('getIndex');
});

想知道它为我们注册了什么路由?我们来加一个小尾巴 ->dd() 就像下面这样:

RouteGroup::make()->controller('AdminController')->go(function ($route) {
    $route->get('/')->as('admin')->uses('getIndex');
})->dd();

虽然对象化了,但总觉的代码量反而更多了是不是?没关系,让我们来个霸气点的:

RouteGroup::make('admin')->as('admin')->before('auth')
    ->controller('AdminController')->go(function ($route) {
        $route->index(  )
              ->create( )
              ->store(  )
              ->edit(   )
              ->update( )
              ->destroy();
    })->dd();

现在有点感觉了?我们还可以做到更多:

RouteGroup::make('admin')->as('admin')->before('auth')
    ->controller('AdminController')->go(function ($route) {
        $route->index(  )
              ->create( )->beforeClear()
              ->store(  )->before('more')->before('more2|more3')
              ->edit(   )->beforeOnly('myself')
              ->update( );
        $route->delete('{id}')->as('destroy')->uses('destroy');
    })->dd();

哦,对了,这里还有个大尾巴 ->ddAll() 它将输出在此之前,由此辅助注册的所有路由信息。

实际用例

请参考此项目 5-say/laravel-4.1-simple-blog