在同一组下添加路由类的更好方法

时间:2017-02-14 08:58:24

标签: laravel-5 laravel-5.3 laravel-5.4

我正在使用Laravel 5.4

路线服务提供商代码低于

Route::prefix('api/v1')
     ->middleware('api')
     ->namespace($this->namespace)
     ->group(base_path('routes/Login.php'));

Route::prefix('api/v1')
     ->middleware('api')
     ->namespace($this->namespace)
     ->group(base_path('routes/Register.php'));

有没有办法在同一组下写两个路径文件?

像这样......

Route::prefix('api/v1')
    ->middleware('api')
    ->namespace($this->namespace)
    ->group(base_path('routes/Login.php'))
    ->group(base_path('routes/Register.php'));

在5.3中。我们可以这样写......

Route::group([
    'middleware'    => 'auth:api',
    'namespace'     => $this->namespace,
    'prefix'        => 'api/v1',
], function ($router) {
    require base_path('routes/API/Driver/Driver.php');
    require base_path('routes/API/Vehicle/Vehicle.php');
});

2 个答案:

答案 0 :(得分:2)

您可以使用与5.4中的5.3相同的解决方案

    Route::group(['prefix' => 'api', /* .... */], function () {

        // extract this to external files as needed
        Route::get('/path', 'HomeController@method');

    });

答案 1 :(得分:1)

group方法将函数作为参数,因此它应该只是

Route::prefix('api/v1')
    ->middleware('api')
    ->namespace($this->namespace)
    ->group(function ($router) {
        require base_path('routes/API/Driver/Driver.php');
        require base_path('routes/API/Vehicle/Vehicle.php');
    });


// routes/API/Driver/Driver.php
<?php

Route::get('/drivers', 'DriverController@method');
// (Route URI is '/api/v1/drivers');