如何确定变量的范围,以便其可用于同一 CFC(CFWheels 插件)中的其他函数?

发布于 2024-12-19 09:26:38 字数 1566 浏览 6 评论 0原文

我想添加一个可由插件中的所有函数访问的变量,但出现变量未定义错误。这是我的插件:

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);
        }
}
  1. 当我在插件内转储 this.secretKey 时,出现变量未定义错误。该错误告诉我 this.secretKeySessions.cfc 控制器中不可用。但不是从 Sessions.cfc 转储,而是从插件的 CFC 转储,如您所见。为什么?

  2. 如何在我的插件中设置 this.secretKey 的范围,以便可以通过 setCookie() 访问它?到目前为止,无论我在函数、伪构造函数还是 init() 中添加定义,variablesthis 都失败了。为了更好地衡量,我添加了 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);
        }
}
  1. When I dump this.secretKey inside the plugin, I get a variable undefined error. The error tells me that this.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?

  2. How can I scope this.secretKey in my plugin so that it can be accessed by setCookie()? So far variables and this have failed, whether I add the definitions in a function, a pseudo-constructor, or the init(). For good measure, I threw in variables.wheels.class.rememberME, to no avail.

Here's the error:

Component [controllers.Sessions] has no acessible Member with name [secretKey]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

つ低調成傷 2024-12-26 09:26:38

生产 模式下,您在 init() 中所做的操作将不起作用。控制器的 init() 仅在对该控制器的第一个请求时运行,因为此后它会被缓存。

因此,this.secretKey 将在该控制器第一次运行时设置,但不会在后续运行中设置。

您有几个选项可以完成这项工作...

I. 使用伪构造函数,它会在每个控制器请求上运行:

component
    extends="Controller"
{
    // This is run on every controller request
    rememberMe("mySecretKey");

    // No longer in `init()`
    public void function init() {}

    public void function remember() {
        // Call the plugin function that creates a cookie / I snipped some code
        setCookie(user.id);
    }
}

II. 使用伪构造函数。使用 before 过滤器来调用每个请求:

component
    extends="Controller"
{
    // No longer in `init()`
    public void function init() {
        filters(through="$rememberMe");
    }

    public void function remember() {
        // Call the plugin function that creates a cookie / I snipped some code
        setCookie(user.id);
    }

    // This is run on every request
    private function $rememberMe() {
        rememberMe("mySecretKey");
    }
}

III.将密钥存储在持久作用域中,以便仅从控制器的 init() 调用一次即可。

component
    mixin="Controller"
{
    public any function init() {
        this.version = "1.0";
        return this;
    }

    public void function rememberMe(string secretKey="rm_#application.applicationName#") {
        application.secretKey = arguments.secretKey;
    }

    public void function setCookie(required string identifier) {
        // This should now work
        writeDump(var=application.secretKey, abort=true);
    }
}

What you're doing in init() isn't going to work when in production mode. A controller's init() 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:

component
    extends="Controller"
{
    // This is run on every controller request
    rememberMe("mySecretKey");

    // No longer in `init()`
    public void function init() {}

    public void function remember() {
        // Call the plugin function that creates a cookie / I snipped some code
        setCookie(user.id);
    }
}

II. Use a before filter to call on every request:

component
    extends="Controller"
{
    // No longer in `init()`
    public void function init() {
        filters(through="$rememberMe");
    }

    public void function remember() {
        // Call the plugin function that creates a cookie / I snipped some code
        setCookie(user.id);
    }

    // This is run on every request
    private function $rememberMe() {
        rememberMe("mySecretKey");
    }
}

III. Store the key in a persistent scope so that calling it only once from the controller's init() is OK.

component
    mixin="Controller"
{
    public any function init() {
        this.version = "1.0";
        return this;
    }

    public void function rememberMe(string secretKey="rm_#application.applicationName#") {
        application.secretKey = arguments.secretKey;
    }

    public void function setCookie(required string identifier) {
        // This should now work
        writeDump(var=application.secretKey, abort=true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文