An overview of Laravel 5 Facades through the Session and Auth facades
Публикувано: 2018-09-29 17:49:14
Facades in Laravel 5 are static interfaces to classes that provide access to objects from Laravel’s service container (features).
To illustrate, by including the following namespace in your file:
use Illuminate\Support\Facades\Session;
You can use Laravel 5’s Session feature which allows you to perform CRUD operations on the user’s session using simply the Session object (you can choose to type the whole namespace instead but that would be bad if you have more than one call to Session):
1 |
if (!Session::get("clientInfo")) { /* do sth */ } |
There are numerous facades which give you different utilities and this article is going to present some of them.
Session
You import the following namespace: use Illuminate\Support\Facades\Session;
A session allows us to store arbitrary properties on our server and match them with a particular user. Behind the curtains, a cookie is stored on the user’s machine which lets us know who the user is and what session properties we have stored for him on our server. The properties will be accessible until the user’s session is active (his session could end as soon as the browser is closed or whatever time limit is set for sessions).
By using Session::put(‘key’, ‘value’) we store an arbitrary key/value pair in the user’s session. We can later retrieve that value by typing Session::get(‘key’). We can also set a default value if the session’s property key is not accessible using Session::get(‘key’, ‘default’). If we have not put the key to the user’s session the above will return to us defaultinstead of value. To remove an item from the user’s session we use the forget static method: Session::forget(‘key’). To check if a particular key is in the user’s session we can use the has method: Session::has(‘key’) – it will return true if the key has been previously set and false otherwise so we can add it to a conditional, such as if.
To view more information on the Session façade visit https://laravel.com/docs/5.0/session
Auth
As you may have guessed, to use the Auth façade you can import it with the following statement:
use Illuminate\Support\Facades\Auth;
Auth::user() will return all data about the currently logged in user in an object (as taken from the database row(s) and associations corresponding to him/her in your database). You can do something like Auth::user()->id or store all user’s data in a variable, whatever is needed in the situation.
{!! csrf_field() !!} is used to place a CSRF token (a hidden input) in the forms you create. By default, forms that use POST need a CSRF token for everything to function.
Auth::check() will return a Boolean. True if the user is currently logged in and false if he is not.
Auth::attempt([’email’ => $email, ‘password’ => $password]) will attempt to log in the user using the email/password combination as taken from some variables and it will return true if he was successfully logged in (so you can use it in conditionals such as if)
Auth::logout() will log the user out and clear all authentication information in the user’s session.
To set up a user log in system, all you need is to set up some routes which Laravel itself will handle and the set the corresponding views according to your own preference. You will also need to type php artisan migrate in order to set up the users table migration (which comes prepared with Laravel).
To see more information on the Auth façade go to https://laravel.com/docs/5.1/authentication
Example
Below is a snippet which exemplifies the usage of the Request and the Session facades. It is a middleware which gets the user’s geolocation from his IP address. The Session façade is used in order to save the geolocation and not call a third-party API to get the user’s geolocation on every page request while the Request façade is used to get the user’s IP address.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php
anmespaceApp\Http\Middleware;
use Closure; use Illuminate\Support\Facades\Request; use Illuminate\Support\Facades\Session;
class getClientInformation { /** * Handle an incoming request. * * @param\Illuminate\Http\Request $request * @param\Closure $next * @return mixed */ public function handle($request, Closure $next) { if (!Session::get("clientInfo")) { $ip= Request::ip() && Request::ip() !== "127.0.0.1" ? Request::ip() : "126.15.33.125"; $data = json_decode(file_get_contents("https://ip-api.com/json/" . $ip)); Session::set("clientInfo", $data); $request->attributes->add(["clientInfo" =>$data]); } $request->attributes->add(["clientInfo" => Session::get("clientInfo")]);
return $next($request); } } |
Conclusion
By now, you probably have seen how you may rely on facades in your own Laravel projects. To view all of the available Laravel 5 facades along with details visit https://laravel.com/docs/5.0/facades
Author Ivan Dimov
Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: https://www.dimoff.biz. facebook, twitter
Article source: https://www.phpgang.com/an-overview-of-laravel-5-facades-through-the-session-and-auth-facades_3645.html