Condition To Rotate Function __Construct

1

I created a __construct function in the Controller.php of Laravel 5.

And this function is obviously running even on the Login screen in Auth .

Is there any way I can put as a condition, run the function inside __construct only when the user is logged in?

I know a way to do, at the beginning of the function I put:

if(Auth::check())
    # Roda Função

But is there another way? That way above every time it will do the verification.

__construct

public function __construct(){
    if(Auth::check()){
        # Monta Menu e Sub Menu
        $menu       = Menu::orderBy('posicao')->get();

        $arrMenu    = array();

        foreach($menu as $itemMenu){
            $arrMenu[$itemMenu->titulo]['titulo']   = $itemMenu->titulo;
            $arrMenu[$itemMenu->titulo]['link']     = $itemMenu->link;
            $arrMenu[$itemMenu->titulo]['icon']     = $itemMenu->icon;
            $arrMenu[$itemMenu->titulo]['slug']     = $itemMenu->slug;

            $submenu            = Submenu::whereIdMenu($itemMenu->id)->orderBy('posicao')->get();

            foreach($submenu as $itemSubMenu){
                $arrMenu[$itemMenu->titulo]['submenu'][$itemSubMenu->titulo]['titulo']  = $itemSubMenu->titulo;
                $arrMenu[$itemMenu->titulo]['submenu'][$itemSubMenu->titulo]['link']    = $itemSubMenu->link;
            }
        }

        # Total de Chamados Agrupado por Status no Menu
        if(Auth::user()->id_role == 1){
            $chamados = DB::table('chamados')
                        ->rightJoin('status', 'status.id', '=', 'chamados.id_status')
                        ->select(DB::raw('count(chamados.id) as total, status, status.id as id_status'))
                        ->orderBy('status.id')
                        ->groupBy('status')
                        ->get();
        }
        else{
            $chamados = DB::table('chamados')
                        ->select(DB::raw('count(chamados.id) as total, status, status.id as id_status'))
                        ->orderBy('status.id')
                        ->groupBy('status')
                        ->rightJoin('status', function($join){
                            $join->on('status.id', '=', 'chamados.id_status')
                                 ->where('id_departamento', '=', Auth::user()->id_departamento)
                                 ->where('id_user', '=', Auth::id())
                                 ->orWhere('id_user', '=', NULL);
                        })
                        ->get();
        }

        $totalFinal = 0;

        foreach ($chamados as $key => $value){
            $totalChamado[$value->id_status] = $value->total;
            $totalFinal                     += $value->total;
        }

        # Coloca em Array o Menu e o Total de Chamados para Colocar no Share
        $arrShare = array(
            'menu'      => $arrMenu,
            'chamados'  => $totalChamado,
            'total'     => $totalFinal
        );

        view()->share('arrShare', $arrShare);
    }
}
    
asked by anonymous 24.11.2015 / 11:23

1 answer

1

There is no other way, as far as I know. If you want to verify that a user is logged in or not, you will always have to check each user request.

In this case, I would not use the __construct method, but the middleware .

namespace App\Http\Middleware;

use Closure;

class IsAuthMiddleware
{

    public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            // Faço a minha mágica aqui
        }

        return $next($request);
    }

}

To configure this, you have to change the AppKernel :

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
]

In the file of routes.php :

Route::get('admin/profile', ['middleware' => 'auth', function () {
    //
}]);
    
24.11.2015 / 11:29