gpt4 book ai didi

php - Laravel 5 Route::group 与公共(public)变量

转载 作者:行者123 更新时间:2023-12-02 17:27:24 24 4
gpt4 key购买 nike

我有这样的代码:

Route::group(['prefix'=>'dashboard'],function(){        
Route::get('addnew',function(){
$user = DB::table('users')->where('username','=',session('username'))->first();
$data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);
return view('layout.addnew')->with($data);
});
Route::get('load',function(){
$user = DB::table('users')->where('username','=',session('username'))->first();
$data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);
return view('layout.load')->with($data);
});
});

但是当我像这样使用公共(public)变量时它不起作用:

Route::group(['prefix'=>'dashboard'],function(){

$user = DB::table('users')->where('username','=',session('username'))->first();
$data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);

Route::get('addnew',function(){
return view('layout.addnew')->with($data);
});
Route::get('load',function(){
return view('layout.load')->with($data);
});
});

请帮帮我!

最佳答案

当您使用闭包时,您需要明确授予对变量的访问权限,因为它们在函数的上下文之外。您可以使用“使用”功能执行此操作。

Route::group(['prefix'=>'dashboard'],function(){

$user = DB::table('users')->where('username','=',session('username'))->first();
$data = array('level' => $user->level, 'name' => $user->name,'email' => $user->email);

Route::get('addnew',function() use($data) {
return view('layout.addnew')->with($data);
});
Route::get('load',function() use($data) {
return view('layout.load')->with($data);
});
});

关于php - Laravel 5 Route::group 与公共(public)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com