Middleware exclusion and inclusion

If it is desired to perform authentication or whitelisting only for certain routes, then the constructor method should be added to the controller and the middleware method of the class could be used as follows:

<?php namespace MyCompanyHttpControllers;

use MyCompanyHttpRequests;
use MyCompanyHttpControllersController;
use IlluminateHttpRequest;
use MyCompanyAccommodationRoom;

class RoomsController extends Controller {

  public function __construct()
  {
    $this->middleware('auth',['except' => ['index','show']);
  }

The first parameter is the key of the $routeMiddleware array in the Kernel.php file. The second parameter is a key and value array. The options are either except or only. The except option is obviously the exclusion, while the only option is the inclusion. In the preceding example, the auth middleware will be applied to all methods except for the index or show methods, which are the two read methods (they do not modify the data). Instead, if the log middleware should be applied on index and show, then the following constructor would be used:

  public function __construct()
  {
    $this->middleware('log',['only' => ['index','show']);
  }

As expected, both approaches are applied as follows and the whitelist middleware is added in as well:

public function __construct()
{
  $this->middleware('whitelist',['except' => ['index','show']);
  $this->middleware('auth',['except' => ['index','show']);
  $this->middleware('log',['only' => ['index','show']);
}

This code would require authentication and a whitelisted IP address for all non-read operations, while it logs any requests to index and show.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset