Answer the question
In order to leave comments, you need to log in
How to determine the user on which page he is Laravel 5.2?
Hello guys!
How laravel determines which page the user is currently on. And how to organize page visit statistics in Laravel?
For example, I'm on the main page, in the statistics (where not be in the database), we enter that I'm on the main page. If I visited the News page, then we enter the visited page. And how does it even work, if we talk about PHP in general, without using frameworks.
Once I did this on yii2:
In each action of each controller I wrote something like:
Yii::$app->db->createCommand()->update('user', ['location' => 'Page Name', 'last_visited' => time()], ['id' => \Yii::$app->user->identity->id])->execute();
Well, this is a duplication of code - AND THIS IS NOT THE RIGHT DECISION (please don't hate it was a long time ago and it's not true)).
Answer the question
In order to leave comments, you need to log in
Thank you all for your attention!
Everything is very simple, wrote like this:
<?php
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Illuminate\Support\Facades\Auth;
class Authenticate
{
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->guest()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('login');
}
} else{
$user = User::find(Auth::user()->id);
$user->last_page = $request->fullUrl();
$user->last_time = time();
$user->save();
}
return $next($request);
}
}
MiddleWare
I call the method
I $request->fullUrl();
Well, Yii2 has a thing called behaviors(). This time. There is also beforeAction() - these are two. In any of these cases, you write your visit logging inside the method. For Laravel, I think there is something similar.
Для определения на какой странице нужно вот тут читать: https://laravel.com/docs/5.2/routing
В двух словах параметры роутов можно обрабатывать с помощью middleware, так-же есть именованные роуты и т.д. в общем куча вариантов и механизмов для этой задачи.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question