Reputation: 3484
I'd like to study users behaviors by recording in a specific table of the database any actions made by the users (user, timestamp, [controller and method names] or [url and request method]).
This thread deals with a similar question but it seems to be listening to three methods ; I'd rather prefer a more global solution.
So, here is what I tried so far:
global.php
Event::listen('*', function(){
$action = Event::firing();
App::make('SpyController')->spy($action);
});
SpyController.php
class SpyController extends BaseController {
public function spy($action){
$spy = new Spy();
$spy->name=$action;
$spy->user_id=Auth::user()->id;
$spy->save();
}
}
Doing so, I get a timeout error. I guess that there are too many things to listen to, and I should better limit to a choice of controllers. However, I don't understand how to listen to specific controllers. The documentation presents an example with auth.login
. If I got it right, this stands for model.method
. Is it possible to listen to controller.method
?
Upvotes: 2
Views: 2528
Reputation: 1360
Timeout error I thought because you have a controller LogController instead of SpyController
auth.login, auth.attempt, router.* are builti-in laravel src and fires with dispatcher. So, if you need to make a listner for controllers you can create a middleware, which will fire an event with enything you want. For example: middleware registred name - spylogs. Handle method(in middleware):
public function handle($request, Closure $next)
{
$method = $request->method();
Event::fire('some_name.'.$method);
return $next($request);
}
In controller:
$this->middleware('spylogs');
Wherever you want:
Event::listen('some_name.method_name', callback);
Upvotes: 2