Come si presenta una variabile in modo che sia disponibile per altre funzioni nello stesso CFC (un plug -in CFWheels)?

StackOverflow https://stackoverflow.com/questions/8370109

  •  27-10-2019
  •  | 
  •  

Domanda

Voglio aggiungere una variabile a cui è possibile accedere a tutte le funzioni in un plug -in, ma ricevo un errore non definito variabile. Ecco il mio 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;
    }
}

Chiamo il plug -in dal mio controller 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. Quando scarico this.secretKey All'interno del plugin, ricevo un errore variabile indefinito. L'errore me lo dice this.secretKey non è disponibile in Sessions.cfc controller. Ma non sono Dumping from Sessions.cfc, sto scaricando dal CFC del plugin, come puoi vedere. Come mai?

  2. Come posso arricchire this.secretKey nel mio plugin in modo che sia possibile accedere da setCookie ()? Finora variables e this non sono riusciti, sia che aggiungo le definizioni in una funzione, uno pseudo-costruttore o l'init (). Per buona misura, ho gettato dentro variables.wheels.class.rememberME, inutilmente.

Ecco l'errore:

Component [controllers.Sessions] has no acessible Member with name [secretKey]
È stato utile?

Soluzione

Cosa stai facendo init() Non funzionerà quando è dentro production modalità. Un controller init() viene eseguito solo sulla prima richiesta per quel controller perché viene memorizzato nella cache.

Così this.secretKey sarà impostato sulla prima esecuzione di quel controller ma mai per le corse successive.

Hai alcune opzioni per far funzionare questo ...

I. Usa lo pseudo-costruttore, che esegue su ogni richiesta del controller:

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. Utilizzare un filtro prima per chiamare ogni richiesta:

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. Memorizza la chiave in un ambito persistente in modo che chiamarla solo una volta dal controller 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);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top