1

プラグインのすべての関数からアクセスできる変数を追加したいのですが、変数未定義エラーが発生します。これが私のプラグインです:

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、変数の未定義エラーが発生します。このエラーは、 Sessions.cfcコントローラーthis.secretKeyで使用できないことを示しています。しかし、ご覧のとおり、Sessions.cfc からダンプしているのではなく、プラグインの CFC からダンプしています。なんで?

  2. this.secretKeysetCookie() でアクセスできるようにプラグインをスコープするにはどうすればよいですか? これまでのところ、定義を関数、疑似コンストラクター、または init() に追加するかどうかにかかわらず、失敗していますvariablesthisおまけに、私は を投入しましたvariables.wheels.class.rememberMEが、役に立ちませんでした。

エラーは次のとおりです。

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

1 に答える 1

2

モードでは、あなたがしていることinit()は機能しません。productionコントローラー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);
    }
}

Ⅱ.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()で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);
    }
}
于 2011-12-05T03:15:48.420 に答える