laravel 类应用程序\Http\控制器\AdminController不存在

uttx8gqw  于 2023-01-27  发布在  其他
关注(0)|答案(9)|浏览(262)

我有这个错误,当用户从登录返回到主页,我不明白这是什么,我正在做的多认证在laravel 5,也帮助我,如果我可以使用登录功能和其他注册功能,已经提供给用户表,我不知道如何做到这一点与已经编写的函数
这是我的登录功能

public function login()
    {
        $data = Input::all();

        // Applying validation rules.
        $rules = array(
            'email' => 'required|email',
            'password' => 'required|min:6',
        );

        $validator = Validator::make($data, $rules);

        if ($validator->fails()){
            // If validation falis redirect back to login.
            return Redirect::to('admin/login')->withInput(Input::except('password'))->withErrors($validator);
        }
        else {
            $userdata = array(
                'email' => Input::get('email'),
                'password' => Input::get('password')
            );
        }

        // doing login.
        if (Auth::validate($userdata)) {
            if (Auth::attempt($userdata)) {
                return Redirect::intended('admin');
            }
        }
        else {
            // if any error send back with message.
            Session::flash('error', 'Something went wrong');
            return Redirect::to('admin/login');
        }
    }

这是我的AdminController

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

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

class AdminController extends Controller
{
//    public function __construct(){
//
//        $this->middleware('admins');
//    }
    public function index(){
//        $user = Auth::user();
//        dd($user);
        return view('admin.home');
    }
}
uxhixvfz

uxhixvfz1#

问题出在您的命名空间中。在控制器中您有:

namespace App\Http\Controllers\Admin;

所以全名是:

App\Http\Controllers\Admin\AdminController;

得到的误差是

App\Http\Controllers\AdminController;

您必须从名称空间的末尾删除Admin

gblwokeq

gblwokeq2#

从Laravel 8开始,您需要按如下方式定义路径
Route::get('/user', [UserController::class, 'index']);
而不是这个
Route::get('/user', 'UserController@index');

vktxenjb

vktxenjb3#

添加到您的控制器:

protected $guard = 'admin';

public function login()
{
    $data = Input::only(['email', 'password']);

    // Applying validation rules.
    $rules = array(
        'email' => 'required|email',
        'password' => 'required|min:6',
    );

    $validator = Validator::make($data, $rules);

    if ($validator->fails()){
        // If validation falis redirect back to login.
        return Redirect::to('admin/login')
            ->withInput(Input::except('password'))
            ->withErrors($validator);
    }

    if (Auth::->attempt($userdata)) {
        return Redirect::intended('admin');
    }
    return Redirect::to('admin/login')
        ->with('error', 'Something went wrong');
}
cczfrluj

cczfrluj4#

我的路由文件是

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

    // Registration Routes...
    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');

    Route::get('admin', 'AdminController@index');

});
pzfprimi

pzfprimi5#

这是我的config/auth.php文件

<?php

return [

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'admins' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'admins',
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

];
sbdsn5lh

sbdsn5lh6#

命名空间应用程序\Http\控制器\管理员;
使用App\Http\控制器\控制器;
使用照明\Http\请求;
类AdminController扩展控制器{

ipakzgxi

ipakzgxi7#

这个错误可能有很多不同的原因,但我遇到过这样的错误,问题是我没有在文件名中包含.php,所以我把“AdminController”改为“AdminController.php”,问题就解决了,希望这对你有帮助。

yc0p9oo0

yc0p9oo08#

在laravel 9中,您必须在\App\Http\Kernel.php中进行检查
在中间件中,您应该添加“admin”=〉Admin::class,且不要忘记在顶部使用App\Http\Middleware\Admin导入该内容;
如果您编辑函数中间件admin.php

rfbsl7qr

rfbsl7qr9#

加上这两行就解决了我的问题。
命名空间应用程序\Http\控制器\管理员;
使用App\Http\控制器\控制器;- ---〉别忘了加这一行

相关问题