dpka7974 2015-09-15 07:19
浏览 8

Laravel 5无法从视图中获取经过身份验证的用户

I am trying to use laravel authentication service.

The problem is I can successfully register a new user and redirect to the successful page. Nevertheless, I can not get the authenticated user within the successful page.

Below is showing what have done so far.

view

    <form class="form-horizontal" role="form" method="POST" action="/auth/register">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">

    <div class="form-group">
        <label class="col-md-4 control-label">User Name</label>
        <div class="col-md-6">
            <input type="text" class="form-control" name="username" value="{{ old('first_name') }}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-4 control-label">E-Mail Address</label>
        <div class="col-md-6">
            <input type="email" class="form-control" name="email" value="{{ old('email') }}">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-4 control-label">Password</label>
        <div class="col-md-6">
            <input type="password" class="form-control" name="password">
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-4 control-label">Confirm Password</label>
        <div class="col-md-6">
            <input type="password" class="form-control" name="password_confirmation">
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-6 col-md-offset-4">
            <button type="submit" class="btn btn-primary">
                Register
            </button>
        </div>
    </div>
</form>

User Model(register_info)

 /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'register_info';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_id','username', 'email', 'password','city'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * The primary key for the model.
     *
     * @var string
     */
    protected $primaryKey = 'user_id';

Controller:

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    /**
     * When a user is successfully authenticated
     * they will be redirected to $redirectPath
     */
    protected $redirectPath = 'manybotest';

    /**
     * When a user is not successfully authenticated
     * they will be redirected to the $loginPath
     */
    protected $loginPath = '/login';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     *
     * 'email' => 'required|email|max:255|unique:register_info',
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'username' => 'required|max:255',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {

        return User::create([
            'user_id' => uniqid('us'),
            'username' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password'])
        ]);
    }
}

Routes:

  Route::get('manybotest', function () {
        $users = Auth::user();
        return view('manybotest',compact('users'));
    });

Display view(manybotest.blade.php):

  <?php

        var_dump(Session::all());
        var_dump(Auth::check());
        var_dump(Auth::user());
        var_dump(Auth::id());
        var_dump($users);

    ?>

Result:

login_82e5d2c56bdd0811318f0cf078b78bfc => int 0//Session::all()
boolean false//Auth::check()
null//Auth::user()
int 0//Auth::id()
null//$users passed by routes

Question:

I can get the user id from session. Why can't I get the authenticated user? I also try to inspect the inner code of laravel, the user was already set by laravel.

  • 写回答

2条回答 默认 最新

  • douxie2029 2015-09-15 09:38
    关注

    You have to inject Guard and Register in to the constructor.

    use Illuminate\Contracts\Auth\Guard;
    use Illuminate\Contracts\Auth\Registrar;
    
    class AuthController extends Controller
    {
    
        public function __construct(Guard $auth, Registrar $registrar)
        {
            parent::__construct();
    
            $this->auth = $auth;
            $this->registrar = $registrar;
    
            $this->middleware('guest', ['except' => 'getLogout']);
        }
    

    If you look at AuthenticatesAndRegistersUsers, you can see that in the postRegister method, login is called on $this->auth.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?