这是我的代码:
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: ...")
将用于重定向。但是我如何保留该过程并进行重定向?
问题是为什么您需要在请求处理时重新加载页面(在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
.
.