auth :: check in laravel 5 not working

问题描述:

我是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(); 
    }); 
+0

什么是注册/播种机是什么样子? – Jazerix

+0

您是通过迁移创建用户表还是您是否手动执行? – Jazerix

+0

在你的刀片文件中,你应该检查身份验证,像这样:@if(\ Illuminate \ Support \ Facades \ Auth :: check()) – Salar

所以我有同样的问题,直到我参观了http://localhost/home,在登录后。

看来我已经运行了php artisan app:name NameOfApp并且它抛出了命名空间错误。我改变namespace App\Http\Controllers;namespace NameOfApp\Http\Controllers;HomeController.php,它工作正常。

希望这会有所帮助。

将您的路由放在中间件组中以访问会话。像这样:

Route::group(['middleware' => ['web']], function() { 
     Route::get('/', function() { 
      if(Auth::check) return "Welcome" . Auth::user()->email; 

     }); 

    }); 

我希望这有助于...(原谅我不使用视图()用于当用户登录在)