我是laravel 5的新手,嘗試創建一個簡單的身份驗證系統,用戶只需註冊並登錄即可,如果用戶登錄,系統會顯示一條歡迎消息,否則註冊和登錄按鈕。但我試圖檢查用戶是否使用Auth :: check()登錄,雖然我已經使用Auth :: attempt()成功登錄,但它在我的控制器和視圖中都讓我錯誤。我在SO和其他網站上進行了搜索,但沒有找到可行的解決方案,有人說它是laravel 5中的一個錯誤,在某些地方這個問題在他們的代碼中出錯了。我不知道我在這裏做錯了什麼。auth :: check in laravel 5 not working
login.blade.php
@extends('master')
@section('content')
<h1>বাংলার পথে</h1>
@if(Auth::check())
<p>স্বাগতম {{ $username }}</p>
@else
<a class="tb-button all-around-spacing push-left" href="/login">লগ ইন</a>
<a class="tb-button all-around-spacing push-left" href="/register">রেজিস্টার</a>
@endif
@stop
AuthenticationController.php
public function login() {
$validator = Validator::make(Input::all(), array(
'user_email' => 'required|email',
'user_password' => 'required|alphaNum'
));
$niceNames = array(
'user_email' => 'ই-মেইল',
'user_password' => 'পাসওয়ার্ড'
);
$validator->setAttributeNames($niceNames);
if($validator->fails()) {
return Redirect::to('login')->withErrors($validator);
} else {
$remember = Input::get('remember');
if(Auth::attempt(array('user_email' => Input::get('user_email'), 'password' => Input::get('user_password')), $remember)) {
return Redirect::to('/');
} else {
return Redirect::to('login')->withMessage('আপনার ই-মেইল অথবা পাসওয়ার্ড টি ভূল');
}
}
}
public function register() {
$validator = Validator::make(Input::all(), array(
'user_name' => 'required|between:6,16',
'user_email' => 'required|email|unique:users',
'user_password' => 'required|alphaNum|between:6,16|confirmed',
'user_password_confirmation' => 'same:user_password'
)
);
$niceNames = array(
'user_name' => 'ট্রাভেলার নাম',
'user_email' => 'ই-মেইল',
'user_password' => 'পাসওয়ার্ড',
'user_password_confirmation' => 'পাসওয়ার্ড নিশ্চীতকরণ'
);
$validator->setAttributeNames($niceNames);
if($validator->fails()) {
Input::flash();
return Redirect::to('register')->withErrors($validator);
} else {
DB::transaction(function(){
$user = new User();
$user->user_name = Input::get('user_name');
$user->user_email = Input::get('user_email');
$user->password = Hash::make(Input::get('user_password'));
$user->save();
});
return Redirect::to('login');
}
}
HomeController.php
public function index()
{
if(Auth::check()) {
$username = Auth::user()->user_name;
return View::make('home/index')->withUsername($username);
} else {
return View::make('home/index');
}
}
user.php的
<?php
/**
* Created by PhpStorm.
* User: Shuvro
* Date: 6/20/15
* Time: 12:30 AM
*/
namespace App\models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
class User extends Model implements AuthenticatableContract {
use Authenticatable;
protected $table = 'users';
protected $fillable = ['user_name','user_email','user_password','user_password_confirmation'];
protected $hidden = ['user_password','remember_token'];
}
routes.php文件
Route::get('/', array('as' => 'index', 'uses' => '[email protected]'));
Route::get('/register', array('as' => 'register', 'uses' => '[email protected]'));
Route::post('/register', array('as' => 'register', 'uses' => '[email protected]'));
Route::get('/login', array('as' => 'login', 'uses' => '[email protected]'));
Route::post('/login', array('as' => 'login', 'uses' => '[email protected]'));
CreateUserTable
Schema::create('users', function(Blueprint $table) {
$table->increments('user_id');
$table->string('user_name');
$table->string('user_email')->unique('user_email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
什麼是註冊/播種機是什麼樣子? – Jazerix
您是通過遷移創建用戶表還是您是否手動執行? – Jazerix
在你的刀片文件中,你應該檢查身份驗證,像這樣:@if(\ Illuminate \ Support \ Facades \ Auth :: check()) – Salar