Php 当用户';s会话使用Laravel 5.7中的Auth处于活动状态
我正在使用Laravel5.7进行一个项目,我试图在有活动会话时重定向到一个路由。目前我已经尝试将此代码放在我的视图顶部,但它似乎不起作用Php 当用户';s会话使用Laravel 5.7中的Auth处于活动状态,php,laravel,authentication,redirect,laravel-blade,Php,Laravel,Authentication,Redirect,Laravel Blade,我正在使用Laravel5.7进行一个项目,我试图在有活动会话时重定向到一个路由。目前我已经尝试将此代码放在我的视图顶部,但它似乎不起作用 @php if(!empty(Auth::user())) { header('Location: '.route('dashboard')); } @endphp 我的代码有错误吗?或者有没有其他方法来做我正在尝试的事情? 提前谢谢 为什么要在视图中检查这样的内容,您应该在返回视图之前在控制器中检查它 无论如何,这里是你可
@php
if(!empty(Auth::user())) {
header('Location: '.route('dashboard'));
}
@endphp
我的代码有错误吗?或者有没有其他方法来做我正在尝试的事情?
提前谢谢 为什么要在视图中检查这样的内容,您应该在返回视图之前在控制器中检查它 无论如何,这里是你可以在你的刀片模板使用
@if(Auth::user())
<script>window.location = "/dashboard";</script>
@else
<script>window.location = "/login";</script>
@endif
@if(Auth::user())
window.location=“/dashboard”;
@否则
window.location=“/login”;
@恩迪夫
请记住,如果auth::user为空,则表示该用户未注册,但也可以使用auth::guest()进行检查,这表示该用户尚未注册
希望能有帮助 您不需要在视图文件中执行重定向,您可以在路由文件中使用auth中间件,如下所示
<?php
//route which don't required login session
Route::group(['middleware'=>'auth'],function(){
//list of routes which you need to access after login
});
?>
为什么不在控制器中执行此操作?@Ash-b谢谢我将代码放置在控制器中,现在它可以工作了。