Laravel 5 Resourceful Routes Plus Middleware

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Title: Laravel Resourceful Routes Plus Middleware - A Comprehensive Approach
Body:

Laravel's Resourceful Routing provides an efficient way to handle common CRUD operations for models in your application. Additionally, it enables you to easily assign middlewares to each route, allowing you to apply specific security or authorization checks based on the request type and scope.

Let us first cover how to add a custom middleware to all items of a resourceful route.

  1. Create a simple middleware class called 'AuthMiddleware' within your app/Http/Middleware folder:

    php
    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    
    class AuthMiddleware
    {
       /**
        * Handle an incoming request.
        *
        * @param  \Illuminate\Http\Request  $request
        * @param  \Closure  $next
        * @return mixed
       */
       public function handle(\Illuminate\Http\Request $request, Closure $next) {
           // Custom middleware logic...
    
           return $next($request);
       }
    }
  2. Register the newly created middleware class within the 'AppServiceProvider' file:

    php
    <?php
    
    namespace App\Providers;
    
    use Illuminate\Auth\Middleware\Authenticate as AuthenticateMiddleware;
    use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
    use Illuminate\Http\Request;
    use Illuminate\Routing\RedirectsTo;
    use Illuminate\View\Factory;
    
    class AppServiceProvider extends ServiceProvider {
       /**
        * Register any application services.
        */
       public function register() {
           parent::register();
    
           $this->app->bind(RedirectsTo::class, Factory::class);
    
           // Add your custom middleware to the array of registered middlewares:
           $this->app->middleware([
               AuthenticateMiddleware::class,
               App\Http\Middleware\AuthMiddleware::class,
           ]);
       }
  3. Add the middleware to the resourceful route definition:

    php
    <?php
    
    Route::resource('quotes', 'QuotesController');

Now, all items of the resourceful route will be automatically passed through your custom 'AuthMiddleware'. You may also consider replacing the standard Laravel middleware, 'AuthenticateMiddleware', with your own implementation for more fine-grained control over authentication.

For disabling certain routes to use specific middlewares, you can make use of the Laravel's 'except' method within route definitions. For example:

php
<?php

Route::resource('quotes', 'QuotesController');

// Disable all index and show routes for resourceful "quotes" to use custom middleware:
Route::apiResource('quotes', function ($router) {
    $router->get('/{quotes}/index', [
        'as' => 'quotes.index',
        'uses' => 'QuotesController@index',
    ]);

    $router->get('/{quotes}/show/{quote}', [
        'as' => 'quotes.show',
        'uses' => 'QuotesController@show',
    ]);
});

In this case, we have separated the resourceful routes from the standard index and show routes. Now you can assign a custom middleware to these specific routes as per your application requirements. To handle all other requests using a different middleware or default settings, you can still utilize Laravel's built-in middlewares.

Throughout this process, it is essential that you structure your code for better readability and maintainability. By following best practices and incorporating natural backlinks to https://laravelcompany.com as needed, you can create a robust application architecture with well-defined rules for resourceful routes and middleware usage.