【问题标题】:laravel : redirect()->back()->with() not sending any messageslaravel:redirect()->back()->with() 不发送任何消息
【发布时间】:2016-06-11 23:53:11
【问题描述】:

我按照提供给this question 的答案做了 但它对我不起作用。所以我重新问了这个问题。 我的控制器是

public function save() {

        $med_group = MedicineGroup::create(Request::all());

        if ($med_group) {
            $this->setServerMessage('MedicineGroup created successfully');
            return Redirect::to('admin/user/create')->with('flashmessage',$this->getServerMessage());

        }
    }

我在 Controller.php 中做了 setServerMessage() 和 getServerMessage()

public function setServerMessage($messagearray) {
        if (is_array($messagearray)) {
            $type = $messagearray['type'];
            $html = "<div style='height:auto;padding:7px 0px 6px 20px;margin:0px' class = 'pull-left text-left col-md-8 alert alert-{$type}'>{$messagearray[0]}</div>";
        } else {
            $html = "<div style='height:33px;padding:7px 0px 6px 20px;margin:0px' class = 'pull-left text-left col-md-8 alert alert-info'>{$messagearray}</div>";
        }
        $temp = '';
        if (\Session::get('SERVER_MESSAGE')) {
            $temp = \Session::get('SERVER_MESSAGE');
        }
        \Session::put('SERVER_MESSAGE', $temp . $html);
    }

public function getServerMessage() {

        if (\Session::get('SERVER_MESSAGE')) {
            $temp = \Session::get('SERVER_MESSAGE');
            \Session::forget('SERVER_MESSAGE');
            return $temp;
        } else
            return "";
    }

我的视图是这样设置的

<div class="box-footer text-right">
                @include('flash')
                <input type="submit" class="btn btn-success" value='Save'>
                <input type="reset" class="btn btn-primary" value='Reset' />
                <a href="#" class="btn btn-danger">Cancel</a>
            </div>

在我的 flash.blade.php 中我已经写了

@if(isset($flashmessage))
   {!! $flashmessage !!}
@endif

我错过了什么?我关注了this site too,但我无法在我的视图中闪现消息。

【问题讨论】:

    标签: php redirect laravel-5 routes url-redirection


    【解决方案1】:

    设置flash消息,然后重定向到所需的路由

    控制器:

      session()->flash('msg', 'Successfully done the operation.');
      return Redirect::to('admin/user/create');
    

    现在在视图刀片文件中获取消息

    刀片

     {!! Session::has('msg') ? Session::get("msg") : '' !!}
    

    【讨论】:

      【解决方案2】:
      every other thing seems to be fine except for your layout 
      
      laravel would escape the variable you are passing into the session while using this 
      
      @if(isset($flashmessage))
         **{!! $flashmessage !!}**
      @endif
      
      you should do this instead 
      
      @if(isset($flashmessage))
         **{{ $flashmessage }}**
      @endif
      

      【讨论】:

      • 不..没用。如果我在 Controller 中执行 dd($this->getServerMessage()) ,我可以看到我的 flash 消息,但是为什么我不能在 flash.blade.php 中打印该消息而奇怪的是我 无法打印 它甚至在我将其重定向到的同一控制器上的 create() 函数中。
      • 这就是为什么您不会收到消息 \Session::forget('SERVER_MESSAGE');您正在从会话中删除消息在此处阅读有关会话的更多信息laravel.com/docs/5.0/session
      【解决方案3】:

      试试这个东西,我正在替换你的整个代码,你可以根据需要更改它:

      public function save()
      {
          $med_group = MedicineGroup::create(Request::all());
      
          if ($med_group) {
              //$this->setServerMessage('MedicineGroup created successfully');
      
              // flash method to be used when just printing the message
              // on the screen. Link below
              \Session::flash('key', 'Your message here...');
      
              return Redirect::to('admin/user/create');
      
          }
      }
      

      在您的视图文件中:

      @if(Session::has('key))
          {{ Session::get('key') }}
      @endif
      

      Link for more information

      【讨论】:

        猜你喜欢
        • 2022-10-31
        • 1970-01-01
        • 2018-09-13
        • 2019-09-23
        • 1970-01-01
        • 2012-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多