Session::set(“variableName”)=$value;but the problem is that I don”t know where to put this code, “cause I would like to set it for one time (when the guest visite the home page or any other page)?The main idea is to use a global variable to use it in all application controllers, I heared about something related to configuration variables but I”m not sure if it will be a good Idea to use config variables or only the session?Thanks

Đang xem: The global session helper

php laravel session-variables laravel-routing application-variables
Share
Improve this question
Follow
asked Feb 9 “15 at 19:33

*

user3429578user3429578
83333 gold badges1010 silver badges1919 bronze badges
2
Add a comment |

10 Answers 10

Active Oldest Votes
83
The correct syntax for this is:

Session::set(“variableName”, $value);For Laravel 5.4 and later, the correct method to use is put:

Session::put(“variableName”, $value);To get the variable, you would use:

Session::get(“variableName”);If you need to set it once, I”d figure out when exactly you want it set and use Events to do it.

For example, if you want to set it when someone logs in, you”d use:

Event::listen(“auth.login”, function() { Session::set(“variableName”, $value);});

Xem thêm: Đời Sống Tình Dục Của Con Người, 4 Bí Mật Cho Đời Sống Tình Dục Hấp Dẫn Hơn

Share
Improve this answer
Follow
edited Jul 22 at 3:22

*

matiaslauriti
3,46444 gold badges2626 silver badges3535 bronze badges
answered Feb 9 “15 at 19:45

*

user1669496user1669496
28.7k66 gold badges6262 silver badges6262 bronze badges
4
Add a comment |
25

Xem thêm:

I think your question ultimately can be boiled down to this:

Where can I set a long-lived value that is accessible globally in my application?

The obvious answer is that it depends. What it depends on are a couple of factors:

Will the value ever be different, or is it going to be the same for everybody?How long exactly is long-lived? (Forever? A Day? One browsing “session”?)

Config

If the value is the same for everyone and will seldom change, the best place to probably put it is in a configuration file somewhere underneath app/config, e.g. app/config/companyname.php:

10,>;You could access this value from anywhere in your application via Config::get(“companyname.somevalue”)

Session

If the value you are intending to store is going to be different for each user, the most logical place to put it is in Session. This is what you allude to in your question, but you are using incorrect syntax. The correct syntax to store a variable in Session is:

Session::put(“somekey”, “somevalue”);The correct syntax to retrieve it back out later is:

Session::get(“somekey”);As far as when to perform these operations, that”s a little up to you. I would probably choose a route filter if on Laravel 4.x or Middleware if using Laravel 5. Below is an example of using a route filter that leverages another class to actually come up with the value:

// File: ValueMaker.php (saved in some folder that can be autoloaded)class ValueMaker{ public function makeValue() { return 42; }}// File: app/filters.php is probably the best placeRoute::filter(“set_value”, function() { $valueMaker = app()->make(“ValueMaker”); Session::put(“somevalue”, $valueMaker->makeValue());});// File: app/routes.phpRoute::group(<"before" => “set_value”>, function() { // Value has already been “made” by this point. return View::make(“view”) ->with(“value”, Session::get(“somevalue”)) ;});

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *