laravel 框架,方法执行完毕,返回消息的方式
- 作者 : zfajax舫
- |
- 发布时间 : 6年前
- |
- 评论数 : 0
- 查看数 : 89
这里主要讲解4种情况,第一种浏览器访问,成功失败,api访问成功失败
1 浏览器,
成功
return view('', compact()); #直接放回视图层和变量
return redirect()->route('web.user.edit', $user->id)->with('success', '个人资料更新成功!'); #第二种路由跳转并且返回消息
失败
return redirect()->back()->withErrors('用户名或密码错误');
视图
@foreach (['danger', 'warning', 'success', 'info'] as $msg)
@if(session()->has($msg))
<div class="alert alert-{{ $msg }} alert-dismissible fade show" style="position: fixed; left:0;right:0;z-index: 9999;width: 70%;margin:auto;" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
{{ session()->get($msg) }}
</div>
@endif
@endforeach
@if(count($errors)>0)
<div class="alert alert-danger alert-dismissible fade show" style="position: fixed; left:0;right:0;z-index: 9999;width: 70%;margin:auto;" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
@foreach($errors->all() as $error)
{{$error}}
@endforeach
</div>
@endif
2 ajax
成功
return response(200);
失败
return response('该客户下有订单禁止删除', 403);