sunscreem's avatar

Whats the equivalent of old school Globals in L5?

I'm building a basic ecommerce site and I need to remember the visitors currency preference.

(Assuming there is are no user accounts) what the best was to handle this in L5?

In the old days I would have just set a session variable but normal searches on here and google are leading me in wild directions.

0 likes
5 replies
d3xt3r's avatar

You can put that in session in laravel as well. Or alternatively, if you want them to be persisted for long-long time, use cookies. So that visitor, might not have to set them for every session.

sunscreem's avatar

Thanks @premsaurav - can you give a little direction on how - maybe a link to docs or a package that does this already? I'm worried I'll start coding this and find I've gone down the wrong path.

d3xt3r's avatar

I am little confused as to how i can help you?

I need to remember the visitors currency preference.

How are you reading it at present? How does the e-commerce website change this preference?

I can suggest a general way around this,

1> However/ whenever you are asking to set the visitor, her currency preference, set a cookie using jquery/or if its page reload using laravel. (https://laravel.com/docs/4.2/requests#cookies)

2> When second request is made, route it through a custom middleware, where you check for this cookie. If present, from here either you can define the settings as a constant, or a global variable or persist it the session storage, up to you and your use case, or, if not present, set the default value.

martinbean's avatar
Level 80

@sunscreem There’s nothing wrong with setting a preference like this in the session:

Session::put('preferred_currency', 'GBP');

“Old-school globals” wouldn’t be applicable as their value would be lost between HTTP requests.

Please or to participate in this conversation.