把它放在全球可访问的地方(
this post
描述了创建这样一个位置的方法):
function add_flash_message( array $notification){
session()->flash( 'any_notifications', true );
if(
empty( session( 'notification_collection' ) )
){
// If notification_collection is either not set or not a collection
$new_collection = new \Illuminate\Support\Collection();
$new_collection->push([
'notification_title' => $notification['title'],
'notification_message' => $notification['message'],
'notification_type' => $notification['type'],
]);
session()->flash( 'notification_collection', $new_collection );
} else {
// Add to the notification-collection
$notification_collection = \Session::get( 'notification_collection' );
$notification_collection->push( [
'notification_title' => $notification['title'],
'notification_message' => $notification['message'],
'notification_type' => $notification['type'],
]);
session()->flash( 'notification_collection', $notification_collection );
}
}
在工作流中
add_flash_message( [
'title' => 'The file does not exist',
'message' => 'The chosen file/path does not seem to exist.',
'type' => 'danger'
] );
在视图/刀片文件中
@if( !empty( Session( 'any_notifications' ) ) )
@foreach (Session('notification_collection') as $notification)
<div class="alert alert-{{ $notification['notification_type'] }}" role="alert">
<strong>{{ $notification['notification_title'] }}</strong> - {{ $notification['notification_message'] }}
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
</div>
@endforeach
@endif