3

What I want to do is to get the a User's activation status before running any methods and redirect if they're not active. Here's my code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;

class HomeController extends BaseController
{
    public function __construct(){
        parent::__CONSTRUCT();
        $this->middleware('auth');
        //SEE IF ACTIVE, something like auth()->user()->active
}

    public function home()
    {
        return redirect('/home');
    }
}

Look at the comment on the last line of the constructor, how do I do that?

2
  • Doesnt this just get explained in laravel.com/docs/5.4/authentication ? Commented Mar 24, 2017 at 13:40
  • @Loko I could get the authenticated user in the methods, it just doesn't work in the constructor, which is where I want it. Check if the authenticated user is active before running any methods. Make sense? Commented Mar 24, 2017 at 13:43

1 Answer 1

3

From 5.3 onwards, you can't directly access session info in a controllers constructor. You can, though, define a Closure based middleware directly in your controller's constructor. More info in the docs

public function __construct()
    {
        $this->middleware('auth');
        $this->middleware(function ($request, $next) {
            if(Auth::user()->active) {
                return Redirect::route('activate');
            }    
            return $next($request);
        });
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.