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>

推荐答案

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.

要解决这个问题,可以在AdminHomeController文件的顶部添加use Auth;以及其他use语句,或者在Auth的所有实例前面加上反斜杠,比如\Auth,让PHP知道如何从全局名称空间加载它.

Laravel相关问答推荐

运行NPM Prod时出现VUE问题

Laravel本地化错误:"setlocale():参数#1($category)的类型必须是int,给定的是Illuminate\Http\Request"

工厂多对多数据透视表属性

如何在 Laravel 9 中获取带分页的订单列表?

无法找到用户sail:passwd 文件中没有匹配的条目

自从 Laravel 使用 Vite 更新后,我无法运行npm run dev

如何在自引用表上使用 Laravel 的 hasManyThrough

在部署到 AWS 时保持我的环境参数安全

Laravel 5 迁移:Schema::table 方法中不同类型的默认长度

如何在 Laravel 中将秒转换为天小时分钟

如何在 AWS Elastic Beanstalk 上设置和使用 Laravel 调度?

Laravel 5:完整性约束违规:1452 无法添加或更新子行:外键约束失败

Laravel 同步错误

在Lumen框架中启用会话

Guzzle 返回流空体而不是 json 体

Elastic search全文与mysql全文?

Laravel 5 - 从控制器级别的所有请求对象中删除参数

Laravel - 使用 Eloquent 查询构建器在 Select 中添加自定义列

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈:

限制自己重载外部 API 的速率