S
S
sawa42016-10-07 12:20:07
Laravel
sawa4, 2016-10-07 12:20:07

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

5 answer(s)
S
sawa4, 2016-10-10
@sawa4

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);
    }
}

MiddleWareI call the method I $request->fullUrl();
don’t know how correct this is, but I solved the task

A
Anton, 2016-10-07
@karminski

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.

A
Alexander Aksentiev, 2016-10-07
@Sanasol

middleware to the rescue

Александр Аблизин, 2016-10-07
@mcmraak

Для определения на какой странице нужно вот тут читать: https://laravel.com/docs/5.2/routing
В двух словах параметры роутов можно обрабатывать с помощью middleware, так-же есть именованные роуты и т.д. в общем куча вариантов и механизмов для этой задачи.

A
Aset, 2016-10-07
@assets

Напиши middleware c функции
stackoverflow.com/questions/30046691/how-to-get-cu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question