Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

elegant way to log user's actions?

Hello,

I am currently developing a user panel for my application and I need an elegant way to log user's action. So I need to create a method that would save into a database: - username/id (available from current session - easy part) - removed/added - parameter - other informations like timestamp (easy part)

What about the removed/added + parameters - i guess it would involve attaching a service that would get current controller and action, but how?

Thank's in advance.



20.1k
Accepted
answer
edited Apr '14

You can do that in a BaseController. This controller will be extended to provide functionality to all your controllers. Have a look at this:

class BaseController extends \Phalcon\Mvc\Controller
{
    public function beforeExecuteRoute()
    {
        $user       = $this->session->get('username');
        $id         = $this->session->get('user_id');
        $controller = $this->dispatcher->getControllerName();
        $action     = $this->dispatcher->getActionName();
        $params     = $this->dispatcher->getParams();

        // Insert the data in your table here....
}

Have a look at the Dispatcher and its methods. You can get additional information regarding your request from that.



2.5k

so afterwards, i need to extend all my controllers that I want to have that behavior with BaseController! Worked like a charm!

Thank you.

Yes it is always a good idea to extend and use a Base controller. There are a lot of helper functions that can be incorporated there ;)