dongwei1921 2014-10-08 10:41
浏览 103
已采纳

使用Cookie记住Laravel中的用户设置

Previously i asked about cookies in Laravel. I quite understand them now, but i'm running into a wall right now.

I want to/ was told to use a Cookie to 'remember' certain settings a user has made to the view.

Why i want to use Cookie:

I have a back-end on my website with a menubar on the left, filling entire height. But by default the menubar is kind of wide (just like mailchimp has), so i have created a button where the use can narrow the menubar. But, when the user narrows the menubar and goes to another page in the back-end, the menubar resets itself to the default width. I would like my website to 'remember' which kind of menubar the user likes best.

Now, i know how to make a Cookie, i know how to pass it to the next page but, what i do not know and would like to know is: How can i 'trigger' the cookie if the user presses the button to narrow the menubar?

Should this be done in the javascript used to make the narrow button work? I really don't know. I don't want to get some complete code or whatsoever, i just like some input from you guys on how do you do this or how would you do this.

  • 写回答

1条回答 默认 最新

  • douduxia1551 2014-10-10 09:50
    关注

    I would use ajax, on button (settings) click send a post request to your controller which will make a Cookie response.

    If you don't want to force a refresh after for the cookie response you can either cheat and use a '302' type response or else just queue the cookie make. Laravel will perform the cookie make on the next response automatically for you.

    If I may make an extra suggestion:

    I would actually save this in database too and use memcache to load the settings from memory.

    Jereon, please let me know if you need code examples for any of the above. I 'll be happy to prepare them for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?