Tanker Tanker - 2 years ago 193
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".

Routes:

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


foo.com/
shows a regular home page, when users login they get send to
foo.com/home
there I have access to the users information, but if the user goes to
foo.com/contact
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
foo.com/home
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
ContactController.php
:

public function __construct(){
$this->middleware('auth');
}


But by doing so, any body else will have to log in or register in order to access my
/contact
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
/contact
,
/services
,
/products
, 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