We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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.



39.3k
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.



4.0k

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