我正在制作一个带有laravel的网站,该网站为用户和帖子提供了CRUD功能。那部分完成了。 之后我做了一个寄存器功能,这也很有效。
但是当我尝试创建一个登录页面时,有些错误。 一旦我选择了"登录" - 按钮,错误页面就会显示错误: Class' 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');
}
}
我的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 :(得分:2)
您需要添加use Auth;
或使用\Auth::
答案 1 :(得分:1)
答案 2 :(得分:0)
isset(auth()->user()->id)
检查用户是否登录。