4

I am using Laravel 5.4

Route Service Provider code is below

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'));

Is there any way to write both route files under the same group?

Something like this...

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

In 5.3. we could write like this...

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 Answers 2

2

You can use the same solution as in 5.3 in 5.4

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

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

    });
2
  • Please check the latest 5.4 format mention in my question.
    – Pankaj
    Commented Mar 26, 2017 at 13:22
  • group() method does not return anything. so you can not chain on it. but as Alejandro said you can pass a closure. Commented Mar 28, 2017 at 8:58
1
+50

The group method takes a function as argument, so it should just be

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');
3
  • I think, You misunderstood my question....I am trying add multiple php files under same group. Not multiple routes under same group.
    – Pankaj
    Commented Mar 27, 2017 at 18:07
  • @Pankaj yes, I don't think I get what you want... Can you show an example of one of those php files?
    – alepeino
    Commented Mar 27, 2017 at 18:19
  • each php file has their routes.
    – Pankaj
    Commented Mar 27, 2017 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.