Hi!
J'm working right now on some project and have a simple question. How to make global variable in laravel?
J have UserController class, and i would like to create variable $User always on system start. and use this variable as global (every controller will use it). I've tried:
Add to config/app.php to aliases, but I have no idea how it works
2.Create it manually (new [path]) in some autoloaded files - not working too
3.J saw solutions with binding but where to put this code?
@topvillas yea, that is an idea but i would like to load many many things in UserController constructor (load data from DB like name, permissions, notifications), so your idea is good but not in my case. $User variable has to be some kind of "Storage" to keep user info.
And for people who think it's a bad idea - every, EVERY page would use information stored in $User, because it will be an advanced system.
P.S. Caching information is not a good idea, because all information must be loaded dynamically
@SarnaMC Controllers are instantiated before the session is started (and authenticated user is resolved). Unfortunately, this means you will have to access the the user in each of your controller actions where you need it:
class SomeController extends Controller
{
public function someAction(Request $request)
{
// You can access authenticated user via $request->user()
}
}