I use to develop with Drupal. In Drupal there are global variables such as $user or $_view, so you can use these in different modules. I wonder how can I make something like this in Laravel, after the user log in, I can use the global $user in different controller.Except using session is there any other ways to implement this one? Thank you.
Asked
Active
Viewed 888 times
0
-
Google says: `About 16,500 results (0.22 seconds)` – carlodurso Nov 16 '14 at 01:31
-
Global variables are bad. There is often a better way. What exactly do you want to do? – Laurence Nov 16 '14 at 01:33
-
1possible duplicate of [Global variable in laravel 4](http://stackoverflow.com/questions/19952572/global-variable-in-laravel-4) – Nabil Kadimi Nov 16 '14 at 01:34
1 Answers
0
For most constants used globally across the application, storing them in config files is sufficient. It is also pretty simple
Create a new file in the app/config directory. Let's call it constants.php
In there you have to return an array of config values.
return [
'langs' => [
'es' => 'www.domain.es',
'en' => 'www.domain.us'
// etc
]
];
And you can access them as follows
Config::get('constants.langs');
// or if you want a specific one
Config::get('constants.langs.en');
And you can set them as well
Config::set('foo.bar', 'test');

Nacho
- 628
- 1
- 12
- 19