在我的 UserController 中我有以下行:
if (!Gate::check('can:main_page')) {
return response(['error' => ['status' => 403, 'message' => 'You can't access this page']], 403);
}
然后在我的控制器模板中
@include('permissionAlert')
permissionAlert 的定义如下
<style>
.alert {
position: absolute;
bottom: 0;
right: 0;
margin-bottom: 15px;
margin-right: 15px;
z-index: 111;
}
</style>
<script type="text/javascript">
setTimeout(function() {
$('#error-alert').alert('close');
}, 5000);
</script>
@if (isset($errorMessage))
<div class="alert alert-danger" id="error-alert">
<strong>Error! </strong> {!! $errorMessage !!}
</div>
@endif
我的问题是如何将控制器中响应的消息添加到警报刀片模板,而不再次传递它所包含的父模板中的所有变量?
所有变量都将在包含的视图中可用。 文档
Blade 的 @include 指令允许您从另一个视图中包含 Blade 视图。父视图可用的所有变量都将可供包含的视图使用
// Controller
return view('welcome', ['message' => 'Alert info']);
// parent.blade.php
{{ $message }} // Alert info
@include('alert')
// alert.blade.php
{{ $message }} // Alert info