Pass variable in controller

I would like to pass a variable from one controller function to another. In other words, how can I access a variable from another function inside the same controller?

thank

+3
source share
3 answers

As Pascal mentioned, one way is to set the property of an object:

class CategoriesController extends AppController
{

  public $foo = '';  

  public function index()
  {
    $this->foo = 'bar';
  }

  public function view($id = null)
  {
    $baz = $this->foo;

    $this->set('baz', $baz);
  }

}

Or pass it as an argument:

class CategoriesController extends AppController
{

  public function index()
  {
    $foo = "bar";
    $this->view($foo)
  }

  public function view($param)
  {
    $this->set('bar', $param);
  }

}
+4
source

I noticed that the definition of a property in the controller is not constant after subsequent calls to the controller.

However, the definition of a property in a model is constant between calls to controller functions.

+2
source

- , :

?

, :

  • , , , .
  • , , - .
  • ... , , ...
+1

Source: https://habr.com/ru/post/1726140/


All Articles