Laravel Cookies

混江龙づ霸主 提交于 2019-12-13 02:50:57

问题


I've been searching and asking around, but i can't find what i am looking for, or anything similair.

I am looking for a way to pass along certain settings for a user. I have a menu in my back-end which the user can narrow or keep wide. Standard the menu is wide. If a user devides to use the narrow menu, and goes to another page, the menu 'resets' to the default wide state. I'd like to pass along this 'setting' with the user.

I know i have to make a cookie. but what else?? no idea.

!IMPORTANT! I am NOT looking for straight answers (which wont teach me anything) I am looking for suggestions on how to do this and if possible a website attached containing some explanation.

Like i said, i know how to make a cookie, but i do not know how to attach it to whatever.


回答1:


The best option to use cookies using queue method because cookie will be automatically added to response.

You can create multiple cookies for one user but you cannot put optional / different values.

If user will change menu to narrow, you should save cookie with value for example 1, and if he wants menu to be wide again, you can remove the cookie or set it's value to 2.

Each time you load page, you should check if there's a cookie with selected name and if it's value is 1 or different.




回答2:


Laravel Cookies using

$cookie = Cookie::make('name', 'value', 60);
$response = Response::make('Hello World');

return $response->withCookie($cookie);

or

$cookie = Cookie::make('name', 'value', 60);
$view = View::make('categories.list');

return Response::make($view)->withCookie($cookie);

or

$cookie = Cookie::make('name', 'value', 60);

return Redirect::route('home')->withCookie($cookie);


来源:https://stackoverflow.com/questions/26235518/laravel-cookies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!