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

Coldfusion 如何确定变量的范围,使其可用于同一CFC(CFWheels插件)中的其他函数?,coldfusion,cfc,cfwheels,Coldfusion,Cfc,Cfwheels,我想添加一个可以被插件中所有函数访问的变量,但是我得到了一个变量未定义错误。这是我的插件: component mixin="Controller" { public any function init() { this.version = "1.0"; return this; } public void function rememberMe(string secretKey="rm_#application.applicat

我想添加一个可以被插件中所有函数访问的变量,但是我得到了一个变量未定义错误。这是我的插件:

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
    时,我得到一个变量未定义错误。错误告诉我,
    此.secretKey
    会话.cfc控制器中不可用。但是我不是从Sessions.cfc中转储,我是从插件的cfc中转储,正如你所看到的。为什么?

  • 如何在我的插件中定义
    this.secretKey
    ,以便setCookie()可以访问它?到目前为止,
    变量
    都失败了,无论我是在函数、伪构造函数还是init()中添加定义。为了更好地衡量,我加入了
    变量.wheels.class.rememberME
    ,但没有效果

  • 以下是错误:

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

    您在
    init()
    中执行的操作在
    production
    模式下不起作用。控制器的
    init()

    因此,
    此.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);
        }
    }
    
    二,。使用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");
        }
    }
    
    三、 将密钥存储在持久作用域中,以便从控制器的
    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);
        }
    }
    

    请发布您使用的CFML,a)调用init(),b)调用memory()。谢谢,这就是我最后要做的。然后我决定放弃,因为保护一个“记住我”的系统是一件痛苦的事,应该使用一个额外的db列来完成。这使得它成为一个可怕的插件候选。所以,不记得我插件毕竟-遗憾的是,我只有在我完成这该死的事情后才意识到这一点。今日风云:研究!