0

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.

Community
  • 1
  • 1
Jeroen
  • 167
  • 2
  • 16
  • user clicks button > php/javascript reduces width and sets width cookie > user refreshes or loads new page > php/javascript (depending on which one is up to you) checks for cookie on pageload, and if it exists sets width accordingly – Steve Oct 08 '14 at 10:44
  • Use Javascript to set the cookie, and read the cookie from Laravel when rendering the templates to adjust settings accordingly. –  Oct 08 '14 at 10:45
  • @Steve Since a cookie has to be attached to a response, how to set the width cookie in the javascript in the view itself AND attach it to the response? :/ – Jeroen Oct 08 '14 at 10:58
  • 1
    http://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – Steve Oct 08 '14 at 11:01
  • @Steve Thanks ! Also ahve read the mozilla docs. But what i can't make work is, getting the cookies value. On page 1 i set/make the cookie, but when i go to another page or so, i try to get the cookie, but it does nothing. Am i missing something here? – Jeroen Oct 08 '14 at 11:16
  • Im not sure whats causing your problem - how are you trying to access the cookie? in js add `console.log(document.cookie);` into you load function (eg in jquery `$(function(){//here});` and see what you get – Steve Oct 08 '14 at 11:22
  • Somehow i managed to make a cookie which only has a value and no key? :/ Here is what my console.log says: test; cookie1=test – Jeroen Oct 08 '14 at 11:35
  • Whoa my bad! Deleted all cookies possible and went for an easier approach. It works now! :) Thanks @Steve! Will add a answer with solution. – Jeroen Oct 08 '14 at 12:01

1 Answers1

0

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.

Keith Mifsud
  • 1,599
  • 1
  • 16
  • 26