如何使用PHP在进程中间重新加载页面?

时间:2018-04-26 06:59:25

标签: php

这是我的代码:

public function save_problem(Request $request)
{
    $doesnot_turn_on = isset($request->doesnot_turn_on) ? $request->doesnot_turn_on : "";
    $res = setcookie('guarantee_ticket', json_encode(["title"=>$request->problem_title, "description"=>$request->problem_description, "turn_on" => $doesnot_turn_on, "unique_product_id" => $request->unique_product_id]), time() + 200000, "/");
    if ( Auth::check() ){
        return $this->register_guarantee_ticket();
    } else {
        return \redirect()->route('short_register',["des" => route('register_guarantee_ticket')]);
    }
}

public function register_guarantee_ticket()
{
    $problem = json_decode($_COOKIE['guarantee_ticket']);

    .
    .

正如您所看到的,当Auth::check()true时,register_guarantee_ticket()将被调用,而$_COOKIE['guarantee_ticket']尚未定义且它(cookie)需要页面重新加载要定义。

如何使用PHP重新加载该页面?

我知道header("Location: ...")将用于重定向。但是,我如何保持流程并进行重定向?

1 个答案:

答案 0 :(得分:0)

问题是您在处理请求时需要重新加载页面的原因(在HTTP机制中不可能)

所以我有一个想法让你解决这个问题(通过将cookie数据传递给子函数):

public function save_problem(Request $request)
{
    $doesnot_turn_on = isset($request->doesnot_turn_on) ? $request->doesnot_turn_on : "";
    $cookie_data = ["title"=>$request->problem_title, "description"=>$request->problem_description, "turn_on" => $doesnot_turn_on, "unique_product_id" => $request->unique_product_id];
    $res = setcookie('guarantee_ticket', json_encode($cookie_data), time() + 200000, "/");
    if ( Auth::check() ){
        return $this->register_guarantee_ticket();
    } else {
        return \redirect()->route('short_register',["des" => route('register_guarantee_ticket')]);
    }
}

public function register_guarantee_ticket($cookie_data)
{
    $problem = $cookie_data; // No need this assign, but I put it here to point out you should pass cookie data directly to sub-function

    .
    .