2016-02-29 48 views
1

我一样根据提供给this question 答案执导,但它并没有为我工作。所以我重申了这个问题。 我的控制器是laravel:重定向() - >回() - >与()不发送任何消息

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这样

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 ""; 
    } 

取得setServerMessage()和getServerMessage()我的看法是建立这样

<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> 

,并在我的闪光灯。 blade.php我写的

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

我错过了什么?我跟着this site too,但我不能在我看来刷新消息。

回答

0

试试这件事情,我更换整个代码,你可以根据你的需要改变它:

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

+0

感谢你的努力,但它仍然是行不通的 – Saurab

0
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 
+0

都能跟得上。 。 不工作 。如果我在控制器中执行dd($ this-> getServerMessage()),我可以看到我的Flash消息,但是为什么我不能在flash.blade.php中打印那条消息而奇怪的是我**无法打印**它甚至在我将其重定向到的同一个Controller中的create()函数内部。 – Saurab

+0

这就是为什么你不会得到消息\ Session :: forget('SERVER_MESSAGE');您正在从会话中删除消息,请阅读https://laravel.com/docs/5.0/session中有关会话的更多信息 – osleonard

1

设置闪光消息,然后重定向到所需的路线

控制器:

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

现在得到的消息,鉴于刀片文件

刀片

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