laravel5.2 session的问题

WBOY
Release: 2016-06-06 20:14:45
Original
1589 people have browsed it

第一个问题

<code>Route::get('test', function () {
        session(['wechat.oauth_user'=>1]);
        dd(session('wechat.oauth_user'));

    });</code>
Copy after login
Copy after login

输出1,然后我把session(['wechat.oauth_user'=>1]);注释了

<code>Route::get('test', function () {
        //session(['wechat.oauth_user'=>1]);
        dd(session('wechat.oauth_user'));

    });</code>
Copy after login
Copy after login

刷新页面,输出null,这是为什么?

第二个问题

我使用了overtrue/wechat的插件,用插件自带的中间件成功获取了用户信息,然后user路由里也输出了session

<code>    public function handle($request, Closure $next, $guard = null)
        {
            $wechat = app('EasyWeChat\\Foundation\\Application');
    
            if (!session('wechat.oauth_user')) {
                if ($request->has('state') && $request->has('code')) {
                    session(['wechat.oauth_user' => $wechat->oauth->user()]);
                    
                    return redirect()->to(url($request->url(), array_except($request->query(), ['code', 'state'])));
                }
    
                return $wechat->oauth->redirect($request->fullUrl());
            }
    
            return $next($request);
        }</code>
Copy after login
Copy after login
<code>    Route::get('user', function () {
    
            dd(session('wechat.oauth_user'));
        });</code>
Copy after login
Copy after login

在这个基础上,我建立了自己的中间件UserMiddleware

<code>    public function handle($request, Closure $next)
        {
            if(Auth::guard('web')->guest()){
                if (!session('wechat.oauth_user')) {
                    return redirect('user');
                }
                else{
                    User::CreateOrLogin(session('wechat.oauth_user'));
                }
            }
    
    
            return $next($request);
        }</code>
Copy after login
Copy after login

User.php 方法 CreateOrLogin

<code>    public static function CreateOrLogin($data){
            $user=self::getByOpenid($data['id']);
            if(!$user){
                $create=['openid'=>$data['id']];
                $user=self::create($create);
            }
            Auth::guard('web')->login($user);
        }
    
        public static function getByOpenid($openid){
            return self::where('openid','=',$openid)->first();
        }</code>
Copy after login
Copy after login

一切都是ok的,但是每次刷新页面都会执行微信获取用户信息的接口,然后一个个注释掉,发现Auth::guard('web')->login($user);这句注释掉后,刷新页面就不会再跳转到微信,加上去就要跳转到微信获取用户信息接口,感觉像是login会清除掉session一样

问题三

<code>Route::group(['middleware' => ['web']], function () {

    Route::get('test', function () {
        dd(auth()->user());

    });


    Route::get('test2', function () {
        dd(auth()->loginUsingId(1));

    });

});</code>
Copy after login
Copy after login

先访问test2,成功登陆,再访问test,输出null,为什么。。。

问题一和问题二和问题三没有关联,求大神解答

回复内容:

第一个问题

<code>Route::get('test', function () {
        session(['wechat.oauth_user'=>1]);
        dd(session('wechat.oauth_user'));

    });</code>
Copy after login
Copy after login

输出1,然后我把session(['wechat.oauth_user'=>1]);注释了

<code>Route::get('test', function () {
        //session(['wechat.oauth_user'=>1]);
        dd(session('wechat.oauth_user'));

    });</code>
Copy after login
Copy after login

刷新页面,输出null,这是为什么?

第二个问题

我使用了overtrue/wechat的插件,用插件自带的中间件成功获取了用户信息,然后user路由里也输出了session

<code>    public function handle($request, Closure $next, $guard = null)
        {
            $wechat = app('EasyWeChat\\Foundation\\Application');
    
            if (!session('wechat.oauth_user')) {
                if ($request->has('state') && $request->has('code')) {
                    session(['wechat.oauth_user' => $wechat->oauth->user()]);
                    
                    return redirect()->to(url($request->url(), array_except($request->query(), ['code', 'state'])));
                }
    
                return $wechat->oauth->redirect($request->fullUrl());
            }
    
            return $next($request);
        }</code>
Copy after login
Copy after login
<code>    Route::get('user', function () {
    
            dd(session('wechat.oauth_user'));
        });</code>
Copy after login
Copy after login

在这个基础上,我建立了自己的中间件UserMiddleware

<code>    public function handle($request, Closure $next)
        {
            if(Auth::guard('web')->guest()){
                if (!session('wechat.oauth_user')) {
                    return redirect('user');
                }
                else{
                    User::CreateOrLogin(session('wechat.oauth_user'));
                }
            }
    
    
            return $next($request);
        }</code>
Copy after login
Copy after login

User.php 方法 CreateOrLogin

<code>    public static function CreateOrLogin($data){
            $user=self::getByOpenid($data['id']);
            if(!$user){
                $create=['openid'=>$data['id']];
                $user=self::create($create);
            }
            Auth::guard('web')->login($user);
        }
    
        public static function getByOpenid($openid){
            return self::where('openid','=',$openid)->first();
        }</code>
Copy after login
Copy after login

一切都是ok的,但是每次刷新页面都会执行微信获取用户信息的接口,然后一个个注释掉,发现Auth::guard('web')->login($user);这句注释掉后,刷新页面就不会再跳转到微信,加上去就要跳转到微信获取用户信息接口,感觉像是login会清除掉session一样

问题三

<code>Route::group(['middleware' => ['web']], function () {

    Route::get('test', function () {
        dd(auth()->user());

    });


    Route::get('test2', function () {
        dd(auth()->loginUsingId(1));

    });

});</code>
Copy after login
Copy after login

先访问test2,成功登陆,再访问test,输出null,为什么。。。

问题一和问题二和问题三没有关联,求大神解答

检查是否加载了 web 中间件

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template