How to access a function variable in another function

Asked

Viewed 608 times

0

Let’s just say I have the job

public function testeX(){
    $x = 1;
    $this->set('x', $x);
}

And I want to access the value of x in another Function that has a view testY.ctp

public function testeY(){ // Esta é minha view
    //Como faria pra acessar a variavel $x aqui ?
}

I just want to access the value of $x in my test view... because it is the value of $x that has the result of my search that will be shown in a modal. I will go through the value of $x to put in their respective fields.

  • Are both methods of the same class? I say this because of the $this in the first job.

  • Both are on the same controller.

1 answer

1

In Cakephp version 1.2.x:

$vars = ClassRegistry::getObject('view') -> viewVars;
echo $vars['x']; // o nome do índice é o nome da variável que deseja acessar.

To use it is necessary to extend the helper

class NomeDaClasse extends AppHelper {

Starting with version 2:

$this->_View->viewVars['x']; // o nome do índice é o nome da variável que deseja acessar.

For version 1

Although irrelevant because hardly anyone still uses version 1 today, follows the same form as version 1.2.x, only modifying the attribute viewVars for passedArgs

ClassRegistry::getObject('view')->passedArgs;
  • $this->_View->viewVars['x']; I would just change the 'x' by the variable I want to access in the view right ? The rest of the syntax is default. I’ll read about it.

  • Not exactly because viewVars returns the indexes generated by set(). Example: $this->set('x', $x ). Get it? Read the Cakephp documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.