php 在 Laravel 5 中找不到类“App\Http\Controllers\admin\Auth”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28947205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:09:18  来源:igfitidea点击:

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

php

提问by Juned Ansari

i am getting error like Class 'App\Http\Controllers\admin\Auth' not found in laravel 5while login. i am new to laravel so please help me or give me some tutorial link for complete laravel application development with admin side

我在登录时收到类似 Class 'App\Http\Controllers\admin\Auth' not found in laravel 5 的错误。我是 Laravel 的新手,所以请帮助我或给我一些教程链接,以使用管理端进行完整的 Laravel 应用程序开发

Routes.php

路由.php

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

AdminHomeController.php

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

登录名.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>

回答by Dan Smith

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

因为除非您专门导入Auth命名空间,否则您的控制器是命名空间的,PHP 将假定它位于类的命名空间下,从而出现此错误。

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

要解决此问题,请use Auth;AdminHomeController文件顶部添加其他 use 语句,或者Auth像这样使用反斜杠作为所有实例的前缀:\Auth让 PHP 知道从全局命名空间加载它。