Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'App\Http\Controllers\admin\Auth' not found in Laravel 5

I am getting error like Class App\Http\Controllers\admin\Auth not found in Laravel 5 during login.

Routes.php

Route::group(array('prefix'=>'admin'),function(){
    Route::get('login', 'admin\AdminHomeController@showLogin');
    Route::post('check','admin\AdminHomeController@checkLogin');    
});

AdminHomeController.php

<?php namespace App\Http\Controllers\admin;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

class AdminHomeController extends Controller {

    //
    
    public function showLogin()
    {
        return view('admin.login');
    }
    
    public function checkLogin(Request $request)
    {
        $data=array(
            'username'=>$request->get('username'),
            'password'=>$request->get('password')
        );

        if(Auth::attempt($data))
        {
            return redirect::intended('admin/dashboard');
        }
        else
        {
            return redirect('admin/login');
        }
        
    }
    
    public function logout()
    {
        Auth::logout();
        return redirect('admin/login');
    }
    public function showDashboard()
    {
        return view('admin.dashboard');
    }
}

login.blade.php

<html>
<body>
 {!! Form::open(array('url' => 'admin/check', 'id' => 'login')) !!}
        
                <input type="text" name="username" id="username" placeholder="Enter any username" />
                <input type="password" name="password" id="password" placeholder="Enter any password" />
                <button name="submit">Sign In</button>
            
        {!! Form::close() !!}
</body>
</html>
like image 884
Juned Ansari Avatar asked Mar 09 '15 16:03

Juned Ansari


1 Answers

Because your controller is namespaced unless you specifically import the Auth namespace, PHP will assume it's under the namespace of the class, giving this error.

To fix this, add use Auth; at the top of AdminHomeController file along with your other use statements or alternatively prefix all instances of Auth with backslash like this: \Auth to let PHP know to load it from the global namespace.

like image 133
Dan Smith Avatar answered Oct 23 '22 19:10

Dan Smith