Tanker Tanker - 3 years ago 322
PHP Question

Laravel 5.2, how to access users information from any route

I have manage to redirect users to a different views/parts of the site based on the users credentials, say the user want to see their profile, no problem, or add a new "post".


/admin <--- only admins no problem
/myprofile <-- only register users no problem
/admin/reports <-- only admins with certain access... no problem

shows a regular home page, when users login they get send to
there I have access to the users information, but if the user goes to
I lose the users information, it shows like if the user hasn't logged in.

So the user goes to the login and get redirected to the
without logging in because the user is already logged in.

So in order for me to access the users information I have to add this code to

public function __construct(){

But by doing so, any body else will have to log in or register in order to access my
page and we don't want that, do we?

So how can we check if user has log in and what credentials are been use from any page on the site, and at the same time give access to regular guests.

Say we have
, etc. Those pages don't require authentication yet we want to check if a user has logged in and access that information.

Answer Source

In the routes.php file you need to declare each routes that need session into the web middleware, so check if contact route is there inside.

Route::group(['middleware' => 'web'], function(){
   // contact route here
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download