0
votes

Hi I have following route and constructor in my controller i want to check if user is authenticated or not if not then redirect to /warehouse/login page. but for some reasons i am getting Route [login] not defined error.

I am migrating my functions from Laravel 4.2 to Laravel 5.4

Constructor:

public function __construct()
    {
        $this->middleware('auth');
        $this->middleware(function ($request, $next) {
            if (!Auth::check()) {
                $url = URL::current();
                $routeName = Route::currentRouteName();

                if ($routeName != "AdminLogin" && $routeName != 'admin') {
                    Session::put('pre_admin_login_url', $url);
                }
                return redirect('/warehouse/login');
            }

            return $next($request);

        }, array('except' => array('WarehouseAdminLogin', 'WarehouseAdminVerify')));


    }

Routes:

Route::get('/warehouse', 'WarehouseController@index');
Route::get('/warehouse/login', array('as' => 'WarehouseAdminLogin', 'uses' => 'WarehouseController@login'));
2
Your error comes from within $this->middleware('auth'); I guess you don't have Auth::routes(); within your routes file. - nakov
@nakov if i remove that then getting the page isn't redirecting properly error. - Danish Jamshed
so as I said, it is because you are missing the routes in your route file. - nakov
@nakov can you please write here which route i am missing ? - Danish Jamshed
Auth::routes(); in your routes file. This defines those login, register and so on routes which are used within the Auth middleware. - nakov

2 Answers

0
votes

You didnt define your login function.

make a function public function login() {'your code'}

in your WarehouseController

0
votes

Edited: the problem is that you have not a route named login. This error is caused by:

$this->middleware('auth');

because this code in the auth middleware:

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        return route('login');
    }
}

So what to do is remove auth middleware and try again or make a route with login name.