Logging user actions in laravel

I'm trying to log all the actions that users do (login / logout / CRUD) into a log table in my database, and from what I saw, the events look like the right way to do this.

I added a did($action)user model method that logs the database action for this user.

Here is what I have so far:

EventServiceProvider.php

namespace App\Events;

use Illuminate\Support\ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->events->subscribe(new UserEventSubscriber);
    }
}

UserEventSubscriber.php

namespace App\Events;

class UserEventSubscriber
{
    public function login(\User $user)
    {
        return $user->did('logged_in');
    }

    public function logout(\User $user)
    {
        return $user->did('logged_out');
    }

    public function subscribe($events)
    {
        $events->listen('user.login', 'App\Events\UserEventSubscriber@login');

        $events->listen('user.logout', 'App\Events\UserEventSubscriber@logout');
    }
}

To register an action:

Event::fire('user.logout', array(Auth::user()));

I'm still trying to ponder the service providers, so I can be very inactive.

My questions:

1) Is the service provider used correctly or is it?

2) Is there a better approach that does not require manually passing Auth::user()to the event every time?

3) ? , , . .

4) (/admin/*). - ?

5) , , . , ? , ?

+4
1
Is a service provider the right thing to use or this?

, service provider , . , EventServiceProvider service provider app/start/global.php, :

$app->events->subscribe(new Events\UserEventSubscriber);

$app - , , (global.php), service provider - ( , php php), Laravel register, service provider , // .

, Auth:: user() ?

, , Auth::user() , , \Auth::user()->did()

public function login()
{
    return \Auth::user()->did('logged_in');
}

, Laravel IoC __constructor, :

class SomeClass {
    public function __construct(User $user)
    {
        $this->use = $user;
    }
}

User , IoC , , Auth::user()/looged in user , Auth::user()->did() .

? , . .

level, , . , events.

(/admin/*). , - ?

, , , IMO.

, , . , ? , ?

, , logging , : depends. , logging, , , () , : , , (/) log in/out, .

, , , , .

+6

Source: https://habr.com/ru/post/1544605/


All Articles