title: How do I set up middleware in laravel
body: <p>So I have my auth middleware, which is registered in the <code>Http/Kernel.php</code> as:</p>
  
  <pre><code>protected $routeMiddleware = [
      'auth' =&gt; \App\Http\Middleware\Authenticate::class,
      'auth.basic' =&gt; \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
      'guest' =&gt; \App\Http\Middleware\RedirectIfAuthenticated::class,
  ];
  </code></pre>
  
  <p>Next I made a change to the middleware handle function in the Authenticate class:</p>
  
  <pre><code>public function handle($request, Closure $next)
  {
      if ($this-&gt;auth-&gt;check()) {
          $user = $this-&gt;auth-&gt;user();
  
          $currentDateTime     = strtotime('Y-m-d H:i:s');
          $tokenExpirationTile = strtotime($user-&gt;token_expiration);
  
          if ($currentDateTime &lt;= $tokenExpirationTile) {
              return $next($request);
          } else {
              $this-&gt;auth-&gt;logout();
              redirect('home/login')-&gt;with('message', 'Your session has expired. Please login in again');
          }
      } else {
          redirect('home/login')-&gt;with('message', 'Please login before attempting to access that');
      }
  }
  </code></pre>
  
  <p>And finally I created the route:</p>
  
  <pre><code>Route::get('home/dashboard', 'HomeController@dashboard', ['middleware' =&gt; 'auth']);
  </code></pre>
  
  <p>I can visit this route but as a non signed in user I should be redirected.</p>
  
  <p>When I through a <code>dd()</code> in the <code>handle</code> function nothing happens. </p>
  
  <p><strong>How do I get it to run this method on this route?</strong> </p>
  
  <p>Also when it comes to other controllers where you need to  authenticate before each action request how do you say: "before each action, run this method." In rails I would do <code>before_action :method_name</code></p>