title: Laravel how to execute specific function every time a specific controller's functions are used
body: <p>I am running laravel and i just implemented PHP Excel in order to export excels with data. For security reasons i want to redirect the user if his facebook id is no match with "my administrator facebook id".</p>
  
  <p>My controller is named <strong>ExcelController</strong> and it has different functions inside it.</p>
  
  <p>I do not want to use the below code inside every function so i am trying to find something to execute like <code>__construct()</code> - everytime this controller is accessed :</p>
  
  <pre><code>if(Auth::user()-&gt;facebook_id != env('facebook_id_admin_access')) {
        return redirect()-&gt;route('home');
  }
  </code></pre>
  
  <p><code>env('facebook_id_admin_access')</code> is an .env variable(like a constant) with the facebook id i want to give access so it can use the specific controller.</p>
  
  <p>I tried creating a <code>public function __construct(){}</code> in the controller and put the above code block but it doesn't get called.</p>
  
  <p>Is this possible and how? Should i use <code>middleware</code> for that?</p>
  
  <blockquote>
    <p>EDITED QUESTION</p>
  </blockquote>
  
  <p>1) Created a <code>middleware</code> <strong>CheckAdminAccess</strong></p>
  
  <pre><code>   */
      public function handle($request, Closure $next)
      {
  
          if(Auth::user()-&gt;facebook_id != env('FACEBOOK_ID_ADMIN_ACCESS') || !Auth::check()) {
              return redirect()-&gt;route('/');
          }
  
          return $next($request);
      }
  </code></pre>
  
  <p>2) Updated in <code>app\http\kernel.php</code> to</p>
  
  <pre><code>protected $routeMiddleware = [
          'admin_access' =&gt; 'App\Http\Middleware\CheckAdminAccess',
      ];
  </code></pre>
  
  <p>3) Updated in <code>routes.php</code> :</p>
  
  <p><code>Route::get('/output/completed', 'ExcelController@completed')-&gt;middleware('admin_access');</code></p>
  
  <p>But it doesn't seem to work, did i forgot anything?</p>