Laravel 前台登录后别的控制器获取不到 user 信息?

只能在登录的控制器获取user信息

UserController.php:

<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

class UserController extends BaseController
{
    public function register()
    {
        return view('home.user.register');
    }

    public function login(Request $request)
    {
        $this->validate($request, [
            'username' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string', 'min:3'],
            //'captcha' => ['required', 'captcha']
        ],[
            'captcha.required' => '验证码不能为空',
            'captcha.captcha' => '请输入正确的验证码',
        ]);
        $credentials = $request->only('username', 'password');
        if (auth('member')->attempt($credentials,true)) {
            //这里可以获取到用户信息
            return $this->error(auth('member')->user());
            //如果返回成功,html接受消息跳转到首页就获取不到信息了
            return $this->success([]);
        }else{
            return $this->error('失败');
        }
    }
}

写成跳转到首页的话,在IndexController.php 里auth('member')->user()获取不到信息,check()是false;

html:

$.ajax({
            url: "/home/index/login",
            data: $('form[name="login"]').serialize(),
            type: "post",
            dataType: "json",
            success: function(ret) {
                if (ret.code === 200) {
                    document.location.href = "/"
                }else {
                    alert('登录失败');
                }
            }
        });

路由:

<?php
//Route::group(['middleware' => 'page-cache'], function($router) {
Route::group([], function($router) {
    $router->get('/', 'IndexController@index')->name('home.index');

});
Route::get('/home/index/air', 'IndexController@air');

//用户
Route::get('/home/index/register', 'UserController@register');
Route::post('/home/index/login', 'UserController@login');

auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'jwt',
            'provider' => 'member',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'member' => [
            'driver' => 'session',
            'provider' => 'members',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'members' => [
            'driver' => 'eloquent',
            'model' => App\Models\Member::class,
        ],
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
最佳答案

如果你登录用 ajax 请求,那么就不要用 session,可以考虑用 laravel/passpor 或者 jwt

4年前 评论
GeorgeKing (作者) 4年前
flashrick (楼主) 4年前
GeorgeKing (作者) 4年前
GeorgeKing (作者) 4年前
flashrick (楼主) 4年前
flashrick (楼主) 4年前
best辉 4年前
flashrick (楼主) 4年前
sunrise丶 4年前
flashrick (楼主) 4年前
讨论数量: 3
php artisan route:list 

看一下路由有没有设置 Guard

4年前 评论
flashrick (楼主) 4年前
Route::middleware('auth:member')->group(function(Router $router) {
    $router->get('/', 'ArticleController@index')->name('home.index');
});

file

4年前 评论
flashrick (楼主) 4年前
GeorgeKing (作者) 4年前
flashrick (楼主) 4年前
GeorgeKing (作者) 4年前

如果你登录用 ajax 请求,那么就不要用 session,可以考虑用 laravel/passpor 或者 jwt

4年前 评论
GeorgeKing (作者) 4年前
flashrick (楼主) 4年前
GeorgeKing (作者) 4年前
GeorgeKing (作者) 4年前
flashrick (楼主) 4年前
flashrick (楼主) 4年前
best辉 4年前
flashrick (楼主) 4年前
sunrise丶 4年前
flashrick (楼主) 4年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!