简体   繁体   English

cakephp数据库记录器 - 如何访问控制器变量?

[英]cakephp database logger - how to access controller variable?

I am following the cakephp documentation to implement the database logger function. 我遵循cakephp文档来实现数据库记录器功能。 I got everything working after creating the logger class in 在创建了记录器类之后,我完成了所有工作

app/Lib/Log/Engine/DatabaseLogger.php

and add these lines to bootstrap 并将这些行添加到bootstrap

CakeLog::config('otherFile', array(
'engine' => 'DatabaseLogger',
'model' => 'LogEntry',
// ...
));

After this I can log to the database by calling 在此之后我可以通过调用登录到数据库

CakeLog::info(message);

Everything works fine, but then I ran into problem trying to automatically log the user's username and ip address. 一切正常,但后来我遇到问题尝试自动记录用户的用户名和IP地址。 I have searched for many solutions onlien but I do not seems to be able to find an answer. 我已经搜索了许多解决方案onlien但我似乎无法找到答案。 Is it possible to access the controller in the DatabaseLogger class? 是否可以访问DatabaseLogger类中的控制器?

You don't need access to the controller 您无需访问控制器

The information you need is effectively global, you don't need to access the controller object to get it. 您需要的信息实际上是全局的,您无需访问控制器对象即可获取它。

Client IP 客户端IP

You can use the Router::getRequest method to get the current request object and retrieve the client IP from that: 您可以使用Router :: getRequest方法获取当前请求对象并从中检索客户端IP:

$request = Router::getRequest();
$ip = $request->clientIp();

Or simply use the env method: 或者只是使用env方法:

$ip = env('REMOTE_ADDR');

Current User's name 当前用户的名字

For this, just use the static session interface : 为此,只需使用静态会话接口

$name = CakeSession::read('Auth.User.name');

This will hopefully get you going in the right direction. 这有望让你朝着正确的方向前进。 I'm currently in the process of migrating some of my CakePHP apps up to 2.2. 我目前正在将一些CakePHP应用程序迁移到2.2。 I'm not 100% certain on this, but it seems to be about the way to go about this. 我不是100%肯定这一点,但似乎是关于这个问题的方法。 I am thinking about using database logging and I like your idea so let me know how this works out. 我正在考虑使用数据库日志记录,我喜欢你的想法,所以让我知道这是如何工作的。 If it doesn't, provide feedback and I'll dig deeper. 如果没有,请提供反馈,我会深入挖掘。 ;) ;)

Create your LogEntry model (database table) to include fields for the username and the ip address. 创建LogEntry模型(数据库表)以包含用户名和IP地址的字段。

Create an AppController. 创建一个AppController。 In the beforeFilter method place the user's ip address in their session. 在beforeFilter方法中,将用户的IP地址放在其会话中。 The CakeRequest object will contain their IP address. CakeRequest对象将包含其IP地址。

$this->request->clientIp();

In your beforeSave() method for that model, you will collect the username and IP address. 在该模型的beforeSave()方法中,您将收集用户名和IP地址。

To access the username field (assuming you are using Auth component and clientIp as the key to their ip address): 要访问用户名字段(假设您使用Auth组件和clientIp作为其IP地址的密钥):

App::import('Component', 'Session');
$Session = new SessionComponent();
$user = $Session->read('Auth.User');
$ip = $Session->read('clientIp');

Assign these to the right fields so that they will be saved. 将这些分配给正确的字段,以便保存它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM