2015-07-17 5 views
0

Я делаю сайт с laravel, у которого есть функция CRUD для пользователей и сообщений. Эта часть завершена. После этого я сделал регистрационную функцию, которая тоже работала.Laravel: Class 'Auth' не найден

Но когда я пытался сделать страницу входа в систему, это неправильно. Как только я выбираю, "логин" -кнопка страницу ошибки появляется с ошибкой: класса 'Auth' не найден

Мой 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:

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

И мои маршруты:

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

Кто-нибудь может мне помочь?

+0

Вы используете Laravel 4 или 5? – DavidT

+0

Я использую laravel 4 – boikersoik

+0

Вы вообще изменили свой файл 'BaseController.php'? – DavidT

ответ

2

Вам нужно добавить use Auth;

или использовать \Auth::

+0

Где мне нужно добавить использовать Auth ;? – boikersoik

+0

Над классом UserController расширяет BaseController {'в вашем UserController – DavidT

+0

Это должно быть первое утверждение под вашим открытым тегом PHP. –

1

Чтобы использовать Auth фасад, вы должны импортировать его в пространство имен.


Лучшим вариантом является использование the helper function вместо:

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

Почему лучше использовать вспомогательную функцию? – MECU

+0

@MECU просто не нужно импортировать пространство имен. –

Смежные вопросы