我如何访问 laravel 上的会话?

Posted

技术标签:

【中文标题】我如何访问 laravel 上的会话?【英文标题】:how can i access sessions on laravel? 【发布时间】:2021-11-18 09:36:26 【问题描述】:

我可以毫无问题地访问索引函数上的Session::get('LoggedUser')

但是当我向 getProjects() 函数发送 axios 发布请求时,我无法访问会话。

控制器代码:

class ProjectController extends Controller

    public function index()
        //dd(Session::get('LoggedUser'));
        return view('user.project.dashboard');
    

    public function getProjects()
        dd(Session::get('LoggedUser'));
    

组件代码:

mounted()
            axios.post('/api/user/projects')
                .then(response=>
                    console.log(response.data.data)
                )
        

api.php:

Route::prefix('user')->group(function()
    Route::post('projects',[UserProjectController::class, 'getProjects']);
);

【问题讨论】:

【参考方案1】:

对于 axios 配置,您必须将 withCrendetials 选项设置为 true

示例配置

const instance = axios.create(
  withCredentials: true,
  baseURL: API_SERVER
)

window.axios = instance 

检查你的 js 文件夹中的 boostrap.js 文件,你会看到你的 axios 的配置设置。如果你在你的 laravel 设置中使用 vue ui

【讨论】:

以上是关于我如何访问 laravel 上的会话?的主要内容,如果未能解决你的问题,请参考以下文章