title: Laravel 5: Call method in class before the method indicated by the route
body: <p>I'm sure there's a well documented way to do this in Laravel, I'm just missing it or not understanding what I'm reading. I have an application that uses a token for accessing part of the website, rather than a username or password. That token identifies the job, checks the status of the job and then redirects the user to the appropriate page.</p>
  
  <p>This is what I have in my routes.php file so far:</p>
  
  <pre><code>$router-&gt;pattern('token', '[A-Za-z0-9]{32}');
  $router-&gt;pattern('id', '[0-9]{1,11}');
  
  Route::group(['prefix' =&gt; '{token}', 'middleware' =&gt; 'token'], function()
  {
      Route::get('/', 'RedirectController@index');
      Route::get('/first-steps', ['as' =&gt; 'firstSteps', 'uses' =&gt; 'FirstStepsController@index']);
      Route::get('/first-steps/form', ['as' =&gt; 'firstStepsForm', 'uses' =&gt; 'FirstStepsController@form']);
      Route::post('/first-steps/form', 'FirstStepsController@processForm');
      Route::get('/designs', ['as' =&gt; 'designs', 'uses' =&gt; 'DesignsController@index']);
      Route::get('/designs/{id}', ['as' =&gt; 'designShow', 'users' =&gt; 'DesignsController@show']);
  });
  </code></pre>
  
  <p>There are a few other routes that are irrelevant. This all works great, if the user visits the link we give them then <code>RedirectController@index</code> checks the stage of the job and redirects them to the correct URL (currently <code>/first-steps</code> for stage 1 and <code>/designs</code> for stage 2). The problem is if the user bookmarks a page, such as <code>/first-steps</code>, when the project moves on to stage 2, the user can still access the stage 1 pages. I want to be able to have a method that runs before the one indicated by the route that makes sure that they are visiting the correct URL for the stage the job is in, redirect if necessary and then store the job (an eloquent object) in the class (or inject it to the method called). </p>
  
  <p>I've tried creating a <code>before</code> method in the controller but it doesn't get called:</p>
  
  <pre><code>public function before(Request $request, $token)
  {
      $this-&gt;website = Website::where('access_token', '=', $token)-&gt;firstOrFail();
  }
  </code></pre>
  
  <p>As you can see in the router I have a custom middleware that validates the token. This is the code for that middleware:</p>
  
  <pre><code>public function handle($request, Closure $next)
  {
      if ( ! Website::where('access_token', '=', $request-&gt;route()-&gt;parameter('token'))-&gt;count())
      {
          return response('Invalid token.', 401);
      }
      return $next($request);
  }
  </code></pre>
  
  <p>I have a feeling that I can use this middleware to achieve what I need to do, but I'm not sure how to proceed or even if I'm on the right track. Can someone please explain what I need to use to achieve my requirements?</p>