类App \ Http \ Controllers \ AdminController不存在

时间:2016-05-24 09:41:11

标签: php laravel

当用户从登录返回主页时我有这个错误我无法理解我在laravel 5中做MultiAuth是什么,如果我可以使用已经可供用户使用的登录功能和其他注册功能,也可以帮助我表格我不知道如何用已编写的函数来做到这一点

这是我的登录功能

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');
    }
}

5 个答案:

答案 0 :(得分:3)

问题出在您的命名空间中。在Contrroller中你有:

namespace App\Http\Controllers\Admin;

所以全名是:

App\Http\Controllers\Admin\AdminController;

,你得到的错误是:

App\Http\Controllers\AdminController;

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

答案 1 :(得分:2)

从Laravel 8开始,您需要像这样定义路线
Route::get('/user', [UserController::class, 'index']);
代替这个
Route::get('/user', 'UserController@index');

答案 2 :(得分:1)

添加到您的控制器:

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');
}

答案 3 :(得分:0)

我的路线文件是

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');

});

答案 4 :(得分:0)

这是我的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,
        ],
    ],

];