如何确定变量的范围,以便其可用于同一 CFC(CFWheels 插件)中的其他函数?
我想添加一个可由插件中的所有函数访问的变量,但出现变量未定义错误。这是我的插件:
component
mixin="Controller"
{
public any function init() {
this.version = "1.0";
return this;
}
public void function rememberMe(string secretKey="rm_#application.applicationName#") {
this.secretKey = arguments.secretKey;
}
public void function setCookie(required string identifier) {
// Create a cookie with the identifier and encrypt it using this.secretKey
// this.secretKey is not available, though, and an error is thrown
writeDump(this.secretKey); abort;
}
}
我从 Sessions.cfc 控制器调用该插件:
component
extends="Controller"
{
public void function init() {
// Call the plugin and provide a secret key
rememberMe("mySecretKey");
}
public void function remember() {
// Call the plugin function that creates a cookie / I snipped some code
setCookie(user.id);
}
}
当我在插件内转储
this.secretKey
时,出现变量未定义错误。该错误告诉我this.secretKey
在 Sessions.cfc 控制器中不可用。但我不是从 Sessions.cfc 转储,而是从插件的 CFC 转储,如您所见。为什么?如何在我的插件中设置
this.secretKey
的范围,以便可以通过 setCookie() 访问它?到目前为止,无论我在函数、伪构造函数还是 init() 中添加定义,variables
和this
都失败了。为了更好地衡量,我添加了variables.wheels.class.rememberME
,但没有效果。
这是错误:
Component [controllers.Sessions] has no acessible Member with name [secretKey]
I want to add a variable that can be accessed by all functions in a plugin, but I'm getting a variable undefined error. Here's my plugin:
component
mixin="Controller"
{
public any function init() {
this.version = "1.0";
return this;
}
public void function rememberMe(string secretKey="rm_#application.applicationName#") {
this.secretKey = arguments.secretKey;
}
public void function setCookie(required string identifier) {
// Create a cookie with the identifier and encrypt it using this.secretKey
// this.secretKey is not available, though, and an error is thrown
writeDump(this.secretKey); abort;
}
}
I call the plugin from my Sessions.cfc controller:
component
extends="Controller"
{
public void function init() {
// Call the plugin and provide a secret key
rememberMe("mySecretKey");
}
public void function remember() {
// Call the plugin function that creates a cookie / I snipped some code
setCookie(user.id);
}
}
When I dump
this.secretKey
inside the plugin, I get a variable undefined error. The error tells me thatthis.secretKey
is not available in Sessions.cfc controller. But I'm not dumping from Sessions.cfc, I'm dumping from the plugin's CFC, as you can see. Why?How can I scope
this.secretKey
in my plugin so that it can be accessed by setCookie()? So farvariables
andthis
have failed, whether I add the definitions in a function, a pseudo-constructor, or the init(). For good measure, I threw invariables.wheels.class.rememberME
, to no avail.
Here's the error:
Component [controllers.Sessions] has no acessible Member with name [secretKey]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
生产
模式下,您在init()
中所做的操作将不起作用。控制器的init()
仅在对该控制器的第一个请求时运行,因为此后它会被缓存。因此,
this.secretKey
将在该控制器第一次运行时设置,但不会在后续运行中设置。您有几个选项可以完成这项工作...
I. 使用伪构造函数,它会在每个控制器请求上运行:
II. 使用伪构造函数。使用 before 过滤器来调用每个请求:
III.将密钥存储在持久作用域中,以便仅从控制器的
init()
调用一次即可。What you're doing in
init()
isn't going to work when inproduction
mode. A controller'sinit()
is only run on the first request for that controller because it get cached after that.So
this.secretKey
will be set on the very first run of that controller but never for subsequent runs.You have a few options to make this work...
I. Use the pseudo-constructor, which does run on every controller request:
II. Use a before filter to call on every request:
III. Store the key in a persistent scope so that calling it only once from the controller's
init()
is OK.