关键词搜索

源码搜索 ×
×

PHP笔记-laravel框架记录session中flash()的一次应用

发布2022-06-14浏览621次

详情内容

场景说明

我这里有个表单提交页面,他的url是get方法/hasLogoIndex。表单提交是post方法/hashLogoSubmit,提交表单后,验证各个参数是否正确,调用三方http的API,得到结果,然后回给/hasLogoIndex页面。也就是重定向到表单提交页面,并且有些图片方面的提示,也在那个页面显示,这里就可以使用flash了。

具体代码

表单页面函数:

  1. public function index(Request $request){
  2. if($request->session()->exists('hashModel')){
  3. return view('hashLogo.index')->with('hashModel', $request->session()->get('hashModel'));
  4. }
  5. return view('hashLogo.index');
  6. }

表单提交函数:

  1. public function submit(Request $request){
  2. $this->validate($request, [
  3. 'inputString' => 'required|min:6|max:128',
  4. 'logoType' => 'required'
  5. ]);
  6. $type = 32;
  7. if($request->logoType == 'logo64'){
  8. $type = 64;
  9. }
  10. else if($request->logoType == 'logo128'){
  11. $type = 128;
  12. }
  13. else{
  14. }
  15. $hashModel = null;
  16. $client = new Client();
  17. try {
  18. $res = $client->request('GET', 'http://127.0.0.1:8889?string=' . $request->inputString . "&type=" . $type);
  19. $jsonObject = json_decode($res->getBody()->getContents());
  20. $hashModel = new HashModel($jsonObject->img, date('Y-m-d h:i:s', time()), $type);
  21. }catch (GuzzleException $e) {
  22. return redirect()->route('hashLogoIndex');
  23. }
  24. $request->session()->flash('hashModel', $hashModel);
  25. return redirect()->route('hashLogoIndex');
  26. }

原理:

submit中通过$request->session()->flash()把参数写入,这个flash相当于一次性的。取出后就无用了。

index中先看session中有没有flash指定的key,如果有,就说明这个是重定向来的,就把数据给blame前端,然后展示。如果没有就不用传这个参数了。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载