Laravel:找不到“Auth”类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31478275/
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-09-14 11:57:51  来源:igfitidea点击:

Laravel: Class 'Auth' not found

laravelauthentication

提问by boikersoik

I'm making a site with laravel that has a CRUD functie for Users and posts. That part is completed. After that I made a register function, that also worked.

我正在使用 laravel 制作一个网站,该网站具有用于用户和帖子的 CRUD 功能。那部分就完成了。之后我做了一个注册功能,这也有效。

But when I tried to make a Login page some is wrong. As soon as I select the, "login"-button a error page shows up with the error: Class 'Auth' not found

但是当我尝试制作登录页面时,有些错误。一旦我选择了“登录”按钮,就会出现一个错误页面并显示错误:未找到“身份 验证”类

My UserController:

我的用户控制器:

<?php

class UserController extends BaseController {
protected $layout = "layouts.main";

/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function index()
{
    // get all the users
    $users = User::all();

    // load the view and pass the users
    return View::make('users.index') ->with('users', $users);
}

/**
 * Show the form for creating a new resource.
 *
 * @return Response
 */
public function create()
{
    // load the create form (app/views/users/create.blade.php)
    return View::make('users.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @return Response
 */
public function store()
{
    $rules = array(
        'email'     => 'required|email|unique:users',
        'password'  => 'required|min:8'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if($validator->fails()) {
        return Redirect::to('users/create')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    }else{
        //store
        $user = new User;
        $user->email    = Input::get('email');
        $user->password = Input::get('password');
        $user->save();

        // redirect
        Session::flash('message', 'Successfully created User!');
        return Redirect::to('users');
    }
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function show($id)
{
    // get the User
    $user = User::find($id);

    // show the view and pass the user to it
    return View::make('users.show') ->with('user', $user);
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return Response
 */
public function edit($id)
{
    // get the user
    $user = User::find($id);

    // show the edit form and pass the User
    return View::make('users.edit') -> with('user', $user);
}

/**
 * Update the specified resource in storage.
 *
 * @param  int  $id
 * @return Response
 */
public function update($id)
{
    $rules = array(
        'email'     => 'required|email',
        'password'  => 'required|min:8'
    );
    $validator = Validator::make(Input::all(), $rules);

    // process the login
    if($validator->fails()) {
        return Redirect::to('users/' . $id . '/edit')
            ->withErrors($validator)
            ->withInput(Input::except('password'));
    }else{
        //store
        $user = User::find($id);
        $user->email    = Input::get('email');
        $user->password = Input::get('password');
        $user->save();

        // redirect
        Session::flash('message', 'Successfully updated User!');
        return Redirect::to('users');
    }
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return Response
 */
public function destroy($id)
{
    // delete
    $user = User::find($id);
    $user->delete();

    // redirect
    Session::flash('message', 'Successfully deleted the User!');
    return Redirect::to('users');
}

//dit is toegevoegd

public function getRegister() {
    $this->layout = View::make('login.register');
}

public function postCreate() {
    $validator = Validator::make(Input::all(), User::$rules);

    if ($validator->passes()) {
        // validation has passed, save user in DB
        $user = new User;
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));
        $user->save();

        return Redirect::to('login/login')->with('message', 'Thanks for registering!');
    } else {
        // validation has failed, display error messages
        return Redirect::to('login/register')->with('message', 'The following errors occurred')->withErrors($validator)->withInput();
    }
}


public function __construct() {
    $this->beforeFilter('csrf', array('on'=>'post'));
    $this->beforeFilter('auth', array('only'=>array('getDashboard')));
}

public function getLogin() {
    $this->layout = View::make('login.login');
}

public function postSignin() {
    $user = array('email'=>Input::get('email'), 'password'=>Input::get('password'));
    if (Auth::attempt($user)) {
        return Redirect::to('login/dashboard')->with('message', 'You are now logged in!');
    } else {
        return Redirect::to('login/login')
            ->with('message', 'Your username/password combination was incorrect')
            ->withInput();
    }

}

public function getDashboard() {
    $this->layout = View::make('login.dashboard');

}


}

My Login.blade.php:

我的 Login.blade.php:

@include('header')

<h1>Login page</h1>

{{ Form::open(array('url'=>'login/signin', 'class'=>'form-signin')) }}
<h2 class="form-signin-heading">Please Login</h2>

{{ Form::text('email', null, array('class'=>'input-block-level',     'placeholder'=>'Email Address')) }}
{{ Form::password('password', array('class'=>'input-block-level', 'placeholder'=>'Password')) }}
<br><br>

{{ Form::submit('Login', array('class'=>'btn btn-large btn-primary btn-   block'))}}
{{ Form::close() }}

@include('footer')

And my routes:

还有我的路线:

<?php
Route::get('home', function()
{
return View::make('home');
});

Route::get('/', function()
{
return View::make('home');
});



 Route::resource('users', 'UserController');

 Route::resource('posts', 'PostController');

 Route::controller('login', 'UserController');

Anybody who can help me?

谁能帮助我?

回答by nathanmac

You need to add use Auth;

你需要添加 use Auth;

or use \Auth::

或使用 \Auth::

回答by Joseph Silber

To use the Authfacade, you have to import it into your namespace.

要使用Auth外观,您必须将其导入到您的命名空间中。



A better option is to use the helper functioninstead:

更好的选择是使用辅助函数

if (auth()->attempt($user)) {
    //
}