【发布时间】:2019-02-18 05:54:07
【问题描述】:
虽然我在 Laravel 项目中设置了会话索引,但出现错误
未定义索引:用户类型
在用户登录时,我设置了$_SESSION['usertype'] 并重定向到/home url。
但我在设置会话索引时遇到了这个错误。
控制器
public function signin(Request $request)
{
$username = $request->username;
$password = md5($request->password);
$exist = UserTbl::where('username','=', $username)->where('password','=', $password)->get();
if(count($exist) > 0)
{
$_SESSION['usertype'] = $exist[0]->user_type;
return redirect('/home');
}
else
{
return redirect('/');
}
}
public function home()
{
$_SESSION['active_menu'] = '1';
return view('home');
}
路线
Route::get('/home','PrescriptionController@home');
查看出现错误的位置
@if($_SESSION['usertype'] == '1') // Got error in this line
<li>
<a href="{{ url('all-user') }}">
<span>All User</span>
</a>
</li>
<li>
<a href="{{ url('create-user') }}">
<span>Create User</span>
</a>
</li>
@endif
【问题讨论】:
-
像这样使用
isset()函数@if(isset($_SESSION['usertype']) && $_SESSION['usertype'] == '1')。 -
if(count($exist) > 0) { $_SESSION['usertype'] = $exist[0]->user_type;回声 $_SESSION['usertype'] ;死; //检查会话使用类型是否设置...
-
@SayedMohdAli......我已经检查了
$_SESSION['usertype']......并且已经设置好了 -
使用 Laravel 会话类而不是直接访问会话用户全局。我还建议从控制器中的会话中获取值,然后将其传递给视图以保持视图尽可能精简。
标签: php laravel session routes