0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravelでのクッキーの読み書き【個人的なお勉強アウトプット】

Last updated at Posted at 2022-03-26

参考図書

Laravelではクッキーの読み書きの機能も用意されている。

クッキーの値の取得

$変数 = $request->cookie(キー)

クッキーを新たに保存

$response->cookie(キー, 値, 分数)

クッキーの値の保存と値の取得は用意されているオブジェクトが違う。

app/Http/Controllers/HelloController.php
class HelloController extends Controller{
public function index(Request $request){
if($request->hasCookie('msg')){
$msg = $request->cookie('msg');
}else{
$msg = 'Cookieはありません';
}
return view('hello,index', ['msg'=>$msg]);
}
public function post(Request $request){
$msg = $request->msg;
$response = response()->view('hello.index',['msg'=>$msg]); 
$response->cookie('msg',$msg,100)
return $response;
}
}

viewを含めたResponseを用意し、cookieで保存してから、そのResponseをreturnする。
クッキーはクライアント側に保存されるものだから、レスポンスをクライアントに返さないと保持はされない。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?