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

场景说明

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

具体代码

表单页面函数:

public function index(Request $request){

    if($request->session()->exists('hashModel')){

		return view('hashLogo.index')->with('hashModel', $request->session()->get('hashModel'));
    }
    return view('hashLogo.index');
}    

表单提交函数:

public function submit(Request $request){

    $this->validate($request, [
        'inputString' => 'required|min:6|max:128',
        'logoType' => 'required'
    ]);

    $type = 32;
    if($request->logoType == 'logo64'){

		$type = 64;
    }
    else if($request->logoType == 'logo128'){

        $type = 128;
    }
    else{

    }

    $hashModel = null;
    $client = new Client();
    try {
        $res = $client->request('GET', 'http://127.0.0.1:8889?string=' . $request->inputString . "&type=" . $type);
        $jsonObject = json_decode($res->getBody()->getContents());
        $hashModel = new HashModel($jsonObject->img, date('Y-m-d h:i:s', time()), $type);
    }catch (GuzzleException $e) {

        return redirect()->route('hashLogoIndex');
    }


    $request->session()->flash('hashModel', $hashModel);

    return redirect()->route('hashLogoIndex');
}

原理:

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值