Laravel controller construct

<?php

class BaseController extends Controller {

    public function __construct()
    {
        // Closure as callback
        $this->beforeFilter(function(){
            if(!Auth::check()) {
                return 'no';
            }
        });

        // or register filter name
        // $this->beforeFilter('auth');
        //
        // and place this to app/filters.php
        // Route::filter('auth', function()
        // {
        //  if(!Auth::check()) {
        //      return 'no';
        //  }
        // });
    }

    public function index()
    {
        return "I'm at index";
    }
}

For laravel 5.x:

public function __construct()
{
    $this->middleware(function(){
        if (!Auth::check()) return 'NO';
    });        
}

Tags:

Php

Laravel