无法在Laravel中重定向错误消息

时间:2015-11-13 18:01:34

标签: php laravel laravel-5 laravel-5.1

我正在尝试在CPE mac不存在时抛出警告消息。 但我从未见过它。

      if(!$cpe_mac){

            return view('main.lan')
            ->with('error','There is no CPE associated with this account !');

        }

第二次尝试

return Redirect::to('/lan')->with('error','There is no CPE associated with this account !');

页面卡在重定向循环中,出现此错误消息

enter image description here

alert.blade.php

@if ($message = Session::get('success'))

<div class="alert alert-sm alert-block alert-success">
    <button type="button" class="close" data-dismiss="alert">
        <i class="fa fa-times"></i>
    </button>
    <i class="fa fa-check green"></i>

    <strong class="green">{{ nl2br($message) }}</strong>

</div>

@elseif ($message = Session::get('error'))

<div class="alert alert-sm alert-block alert-danger">
    <button type="button" class="close" data-dismiss="alert">
        <i class="fa fa-times"></i>
    </button>

    <strong class="red">{{ nl2br($message) }}</strong>

</div>

@endif

任何线索?

1 个答案:

答案 0 :(得分:2)

所以这就是这个交易:

if(!$cpe_mac){

    return view('main.lan')
        ->with('error','There is no CPE associated with this account !');
}

回复您的观点并附加一个错误变量,因此 alert.blade.php 应如下所示:

//the error part
@elseif ($error)

<div class="alert alert-sm alert-block alert-danger">
    <button type="button" class="close" data-dismiss="alert">
        <i class="fa fa-times"></i>
    </button>

    <strong class="red">{{ nl2br($error) }}</strong>

</div>

@endif

所以您正在使用会话,但该错误变量永远不会出现在会话中。

至于重定向循环,我无法帮助您至少使用您提供的代码,但我猜测问题肯定在这里:

return Redirect::to('/lan')->with('error','There is no CPE associated with this account !');

在您的局域网路由上,您反复重定向,但使用此方法,变量将确实在会话中,因为您将数据重定向并将数据存储为Flash消息,here's the documentation about that

总结一下:

return view('main.lan')
            ->with('error','There is no CPE associated with this account !');

创建变量$ error并加载视图

至于此:

return Redirect::to('/lan')->with('error','There is no CPE associated with this account !');

将数据存储在会话中并重定向到提供的路由,因此在下一个路由中,您可以使用会话外观或session()帮助程序访问数据

相关问题