2
votes

I use the following middleware in routing Laravel:

Route::group(['middleware' => 'web'], function () {

    Route::resource('Order', 'OrderController');
});

When I try to call this path in URL:

http://localhost/web/order

I get an error:

Sorry, the page you are looking for could not be found.

in RouteCollection.php line 161 at RouteCollection->match(object(Request)) in Router.php line 821 at Router->findRoute(object(Request)) in Router.php line 691 at Router->dispatchToRoute(object(Request)) in Router.php line 675 at Router->dispatch(object(Request)) in Kernel.php line 246 at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 at Pipeline->Illuminate\Routing{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 at Pipeline->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 at Pipeline->then(object(Closure)) in Kernel.php line 132 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 at Kernel->handle(object(Request)) in index.php line 53

2

2 Answers

5
votes

Route::group();, as its name stands, is for grouping routes that shares something in common.

'middleware' => 'web' will make all the routes inside share the same group of middleware or share the same middleware. Look at app/Http/Kernel.php.

The middleware will not affect your route URL but how the route is treated in your app.

'prefix' => 'web' will make all your routes share the same path. Which looks more to what you need.

The right code will be:

Route::group(['prefix' => 'web'], function () {
    Route::resource('Order', 'OrderController');
});

The URL to access this route will be:

/web/order

To have both, prefix web and middleware auth with guard api auth:api, the code would be:

Route::group(['prefix' => 'web', 'middleware' => ['auth:api']], function () {
    Route::resource('Order', 'OrderController');
});
1
votes

You should be using 'prefix' => 'web' instead of 'middleware' => 'web' if you are looking for a url like in your post. By default, Laravel 5.2 wraps all routes in the 'web' middleware, don't have to declare it again.

I can't clearly explain what middleware is other than it guards certain routes based on rules in place. Laravel documentation on route prefixes