Reputation: 11642
I have an API based on CakePHP, controller with name AccessLogController is responsible for saving access log into the database.
Question is:
What is the best practice for global logging in CakePHP?
I thought that I will call AccessLogController method from inherited AppController in before filter callback like this:
public function beforeFilter() {
$accessLogCtrl = new AccessLogsController();
$accessLogCtrl->add($param1, $param2);
}
But i not sure that is it a good way how to do it..
Many Thanks for any advice..
Upvotes: 0
Views: 584
Reputation: 60463
That's not how you should use controllers, never ever, except maybe in your test suite!
That being said, using AppController::beforeFilter()
to globally log controller action requests is generally fine, just make sure that you always invoke parent::beforeFilter()
in case you are overriding the filter in an extending controller.
However, you should definitely refactor your logging functionality into either a utility class, a component, a model, or even directly into AppController
, depending on how it's actually logging things, and if you need to use it in places other than in AppController
.
Upvotes: 1