重构Laravel路线

时间:2017-02-22 17:40:46

标签: laravel-5

我正在部分成功地重构普通的laravel路线到一个班级。目前我错误地寻找控制器。

这是我重构的基本模式。我需要vendorsusersterminals

的相同模式
Route::group(['middleware' => ['csrf','DashboardAuthenticate'], 'namespace' => 'App\Http\Controllers'], function () {

    Route::get('vendors/', 'VendorController@index');
    Route::get('vendors/create', 'VendorController@create');
    Route::get('vendors/{id}', 'VendorController@show');
    Route::post('vendors/search', 'VendorController@search');
    Route::put('vendors/', 'VendorController@update');
    Route::delete('vendors/', 'VendorController@destroy');

    Route::get('roles/', 'RoleController@index');
    Route::get('roles/create', 'RoleController@create');
    Route::get('roles/{id}', 'RoleController@show');
    Route::post('roles/search', 'RoleController@search');
    Route::put('roles/', 'RoleController@update');
    Route::delete('roles/', 'RoleController@destroy');

    Route::get('users/', 'UserController@index');
    Route::get('users/create', 'UserController@create');
    Route::get('users/{id}', 'UserController@show');
    Route::post('users/search', 'UserController@search');
    Route::put('users/', 'UserController@update');
    Route::delete('users/', 'UserController@destroy');

    Route::get('terminals/', 'TerminalController@index');
    Route::get('terminals/create', 'TerminalController@create');
    Route::get('terminals/{id}', 'TerminalController@show');
    Route::post('terminals/search', 'TerminalController@search');
    Route::put('terminals/', 'TerminalController@update');
    Route::delete('terminals/', 'TerminalController@destroy');
});

为了重构,我创建了一个类似于:( CIRoute类文件位于App \ Classes \ Routes命名空间下)

class CIRoutes
{
    public static function addRoutes($name)
    {
        $n2 = ucfirst($name);
        Route::get($name.'s/', $n2.'Controller@index');
        Route::get($name.'s/create', $n2.'Controller@create');
        Route::get($name.'s/{id}', $n2.'Controller@show');
        Route::post($name.'s/search', $n2.'Controller@search');
        Route::put($name.'s/', $n2.'Controller@update');
        Route::delete($name.'s/', $n2.'Controller@destroy');
    }
}

然后我创建路线:

CIRoutes::addRoutes('vendors');

我认为这样做得很好,但我错了。目前php artisan route:listClass App\Http\Controllers\RolesController does not exist回来了 - 我的意思是它在没有重构的情况下工作正常。

如何重构常用路由模式?

1 个答案:

答案 0 :(得分:0)

我认为将路由存储在 routes / web.php 文件而不是类中是更好的选择,并使用group方法来组织它们。

Route::group(['prefix' => 'vendors'], function () {

    Route::get('/', function ()    {
        // Matches The "/vendors/" URL
    });
    Route::get('/create', function ()    {
        // Matches The "/vendors/create" URL
    });

});

Group还允许中间件,子域和命名空间路由。

接下来,由于您的班级正在创建典型的CRUD路线,您只需使用:

Route::resource('vendors', 'VendorsController');

创建以下路线:

Verb        URI                     Action        Route Name
GET         /vendors                index         vendors.index
GET         /vendors/create         create        vendors.create
POST        /vendors                store         vendors.store
GET         /vendors/{vendor}       show          vendors.show
GET         /vendors/{vendor}/edit  edit          vendors.edit
PUT/PATCH   /vendors/{vendor}       update        vendors.update
DELETE      /vendors/{vendor}       destroy       vendors.destroy

请注意,Laravel会自动将 routes / web.php 中的所有路由映射到您的' web' App \ Providers \ RouteServiceProvider 类中的 App \ Http \ Controllers 命名空间。

/**
 * Define the "web" routes for the application.
 *
 * These routes all receive session state, CSRF protection, etc.
 *
 * @return void
 */
protected function mapWebRoutes()
{
    Route::group([
        'middleware' => 'web',
        'namespace' => $this->namespace,
    ], function ($router) {
        require base_path('routes/web.php');
    });
}

因此,如果您仍然希望使用类来定义路由,则必须对类中的路由进行分组,并为它们分配App \ Http \ Controllers命名空间和' web'中间件。

Web中间件确保这些路由提供会话状态和CSRF保护等功能。

所以你可能(我还没有测试过)做类似的事情:

public static function addRoutes($name)
{

    Route::group([
        'middleware' => 'web',
        'namespace' => 'App\Http\Controllers',
    ], function () use ($name) {

        $n2 = ucfirst($name);
        Route::get($name.'s/', $n2.'Controller@index');
        Route::get($name.'s/create', $n2.'Controller@create');
        Route::get($name.'s/{id}', $n2.'Controller@show');
        Route::post($name.'s/search', $n2.'Controller@search');
        Route::put($name.'s/', $n2.'Controller@update');
        Route::delete($name.'s/', $n2.'Controller@destroy');

    });

}