Php 通过在laravel中发送数据重定向

Php 通过在laravel中发送数据重定向,php,laravel,laravel-5.5,Php,Laravel,Laravel 5.5,我尝试了以下代码:若要登录的用户信息不正确,请再次将用户重定向到loin页面,并显示一条消息: if (!$info_is_correct){ $_SESSION['err_msg'] = 'your login info is not correct'; return redirect('http://localhost:8000/user/login'); } 这是我在登录页面(视图)中响应错误消息的操作: @isset($_SESSION['err_msg'])

我尝试了以下代码:若要登录的用户信息不正确,请再次将用户重定向到loin页面,并显示一条消息:

if (!$info_is_correct){
    $_SESSION['err_msg'] = 'your login info is not correct';
    return redirect('http://localhost:8000/user/login');
}
这是我在登录页面(视图)中响应错误消息的操作:

    @isset($_SESSION['err_msg'])
        <div class="alert alert-danger mt-2" style="font-family:IRANSans;font-size:16px ;">
            <p>
                <strong>خطا !</strong>
                {{$_SESSION['err_msg']}}
                <?php unset($_SESSION['err_msg']) ?>

            </p>
        </div>
    @endisset
@isset($\u会话['err\u msg']))


{{$\u会话['err\u msg']}

@尾端集
问题在哪里?通过重定向将数据发送到视图的最佳方式是什么?

使用以下方法:

如果是视图:

@if (session('err_msg'))
    <div>{{ session('err_msg') }}</div>
@endif
@if(会话('err_msg'))
{{会话('err_msg')}
@恩迪夫
使用以下方法:

如果是视图:

@if (session('err_msg'))
    <div>{{ session('err_msg') }}</div>
@endif
@if(会话('err_msg'))
{{会话('err_msg')}
@恩迪夫

由于您使用的是laravel,因此可以使用并返回

redirect()->back()->withErrors($validator)

从视图中,您可以访问包含所有错误的
$errors
变量

您可以循环遍历errors变量并打印出错误

从您的登录视图

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
@if($errors->any())
    @foreach($errors->all()作为$error)
  • {{$error}}
  • @endforeach
@恩迪夫
由于您使用的是laravel,因此可以使用并返回

redirect()->back()->withErrors($validator)

从视图中,您可以访问包含所有错误的
$errors
变量

您可以循环遍历errors变量并打印出错误

从您的登录视图

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
@if($errors->any())
    @foreach($errors->all()作为$error)
  • {{$error}}
  • @endforeach
@恩迪夫
您可以这样做,使用use输入发送错误消息

return redirect('http://localhost:8000/user/login')->with('err_msg', 'Your error message')->withInput();
资料来源:


注意:您也可以处理错误消息

您可以这样做,使用use输入发送错误消息

return redirect('http://localhost:8000/user/login')->with('err_msg', 'Your error message')->withInput();
资料来源:


注意:您也可以使用错误消息

在laravel中使用
会话
方法设置会话值,请确保使用,因为它是全局的,并且为整个站点设置会话值

同时删除
http://localhost:8000
在开发和生产过程中使用此路线

if (!$info_is_correct){
    session('err_msg', 'your login info is not correct');
    return redirect('/user/login');
}
并从会话中获取值作为


使用
session
方法在laravel中设置会话值,确保使用,因为它是全局的,并为整个站点设置会话值

同时删除
http://localhost:8000
在开发和生产过程中使用此路线

if (!$info_is_correct){
    session('err_msg', 'your login info is not correct');
    return redirect('/user/login');
}
并从会话中获取值作为


谢谢你有用的回答。但是有一个问题,我可以将会话('something')作为变量$something吗?@Mahdi您可以将其作为变量
$something=session('something')
传递给视图,但您不需要这样做,因为
session()
是一个全局可访问的帮助程序。我的意思是在刀片模板中使用$err_msg代替session('err_msg')。意味着在重定向之前做一些事情。我可以吗?谢谢你有用的回答。但是有一个问题,我可以将会话('something')作为变量$something吗?@Mahdi您可以将其作为变量
$something=session('something')
传递给视图,但您不需要这样做,因为
session()
是一个全局可访问的帮助程序。我的意思是在刀片模板中使用$err_msg代替session('err_msg')。意味着在重定向之前做一些事情。我可以吗?谢谢。但当我做这项工作并重定向到登录页面并显示错误时,当我按键盘上的f6按钮刷新页面时,错误仍然显示出来。第二次刷新后,将隐藏错误。如何显示一次错误消息?您需要始终显示错误消息(我的意思是在多次刷新后)?不,仅显示一次。上面的代码显示错误两次表示2次刷新谢谢。但当我做这项工作并重定向到登录页面并显示错误时,当我按键盘上的f6按钮刷新页面时,错误仍然显示出来。第二次刷新后,将隐藏错误。如何显示一次错误消息?您需要始终显示错误消息(我的意思是在多次刷新后)?不,仅显示一次。上述代码显示两次错误意味着两次刷新