我是laravel 5中的新手,并尝试创建一个简单的身份验证系统,用户只需注册并登录,如果用户登录,则会显示欢迎消息,否则会显示注册和登录按钮。但是我试图检查用户是否使用Auth :: check()登录,虽然我已经使用Auth :: attempt()成功登录,但它在我的控制器和视图中都是假的。我在SO和其他网站上搜索过,但发现没有可行的解决方案,某处说它是laravel 5中的一个错误,某个地方的问题只是代码中的错误。我不知道我在这里做错了什么。
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' => 'HomeController@index'));
Route::get('/register', array('as' => 'register', 'uses' => 'AuthenticationController@registerView'));
Route::post('/register', array('as' => 'register', 'uses' => 'AuthenticationController@register'));
Route::get('/login', array('as' => 'login', 'uses' => 'AuthenticationController@loginView'));
Route::post('/login', array('as' => 'login', 'uses' => 'AuthenticationController@login'));
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();
});
答案 0 :(得分:0)
所以在我登录后访问http://localhost/home
之前,我遇到了同样的问题。
似乎我运行了php artisan app:name NameOfApp
并且它抛出了命名空间错误。我在namespace App\Http\Controllers;
中将namespace NameOfApp\Http\Controllers;
更改为HomeController.php
,但效果很好。
希望这有帮助。
答案 1 :(得分:0)
将您的路由放在中间件组中以访问会话。像这样:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
if(Auth::check) return "Welcome" . Auth::user()->email;
});
});
我希望这会有所帮助......(原谅我在用户登录时没有使用view())