How to redirect to login if user is not authenticated in laravel?

0 votes

I'm trying to use the __constructor from the extended class (AdminController extends AdminBaseController) but aparently it's not working and I have no idea of what can be, here you can see both of my classes:

AdminBaseController.php

class AdminBaseController extends Controller
{
    public function __construct(){
        if (!Auth::user()){
            return view('admin.pages.login.index');
        }
    }
}

AdminController.php

class AdminController extends AdminBaseController
{
    public function __construct(){
        parent::__construct();
    }

    public function index()
    {
        return view('admin.pages.admin.index');
    }

    public function ajuda()
    {
        return view('admin.pages.admin.ajuda');
    }
}
Oct 28 in Laravel by kartik
• 35,300 points
76 views

1 answer to this question.

0 votes

Hello @kartik,

Use middleware for this purpose and then in controller constructor use it as in example below.

public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}

And then you need to secure routes where you want from user to be logged in to access.

Route::group(['middleware' => 'auth'], function() {
      Route::get('/dashboard', 'DashboardController@index');
});

Hope it helps!!

Thank You!!

answered Oct 28 by Niroj
• 76,540 points

Related Questions In Laravel

0 votes
1 answer

How to identify wheather the request is HTTP GET or HTTP POST in Laravel?

Hey, In order to identify the type of ...READ MORE

answered Mar 19 in Laravel by Niroj
• 76,540 points
130 views
0 votes
1 answer

What is database migration and how to create database migration in Laravel?

Hii, Migrations are like version control for your database, ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
339 views
0 votes
1 answer

Error:login in Laravel. How to solve?

Hey, First check if your model login has a field password in ...READ MORE

answered Mar 26 in Laravel by Niroj
• 76,540 points
807 views
+1 vote
1 answer

How to check if a record already exists in a laravel?

Hey, As a newbie it is most common ...READ MORE

answered Mar 31 in Laravel by Niroj
• 76,540 points
6,074 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
3,434 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
4,140 views
0 votes
1 answer

How to validate an input field if value is not null in Laravel?

Hello @kartik, try using nullable as a rule 'password' ...READ MORE

answered Sep 25 in Laravel by Niroj
• 76,540 points
430 views
0 votes
1 answer

How to check request is ajax or not in Laravel?

Hello, Laravel allow use of their library method that ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
317 views