Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass variable within controller

Tags:

php

cakephp

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

Thanks

like image 361
octavian Avatar asked Jul 27 '26 03:07

octavian


1 Answers

As Pascal mentioned, one way is to set a property on the 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 argument:

class CategoriesController extends AppController
{

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

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

}
like image 165
Darren Newton Avatar answered Jul 29 '26 16:07

Darren Newton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!