How to pass variables to another Action

I want to use a variable in another Action in my controller.

Example :

public function testAction() {

  $test = 12345;
  ...

}

public function anotherAction() {

  var_dump($test); // I want the $test value here, then 12345
  ...

}

How can I get this ?



9.8k
    class YourClassName extends Controller {

        protected $test;

        public function testAction() {
            $this->test = 12345;
        }

        public function anotherAction() {
            var_dump($this->test);
        }
    }


60.9k
edited Oct '15

now... I wrote the same answer as Zach, but never the less

  1. You can use the dummest wayh ever by storing $test into the session
    $_SESSION[test]=1234;

    var_dump($_SESSION["test"]); // at any place among your app

this probably is the worse possible scenario ever.

  1. Depends on the case you can use Cookies, generaly it is also a bad idea, for many reasons. But if the data in this variable is not sensitive you may go for it.

  2. If there is a form in the view you can

    public function testAction() {

          $this->view->setVar("MYVAR","1234");

      }

inside the view:

      <form method="post" bla bla bla>
          <input type="hidden" name="test" value="<?php echo $this->view->test" />
      </form>

and in the controller action

      public function anotherAction() {
          print_r($_POST); //$_GET
      }
  1. You can store this value in the database ( depends on the case ) or some mem cache store ( like Redis )

// i was going deep in details but this markdown editor is only screwing with me, i am too nervous to continue now.. let me know if you want more details



12.6k

I have already try to do as Zach but the value is lost.

Izo : I can't send the variable to my view because it is not secure. My variable will content a secret token and if someone check the HTML code with firebug this guy can catch the token.



60.9k

if it is that secude, go for session, just remember there is a timer ;-)



12.6k

There is no other ways to do this ? :-l



60.9k

what's your worries about the session ?



9.8k

Don't use session... It's not good practice.

class YourClass extends Controller {
  protected $test;

  protected function setTest($val) {
    $this->test = $val;
  }

  protected function getTest() {
    return $this->test;
  }

  public function firstAction()  {
    $this->setTest(12345);
  }

  public function secondAction() {
    var_dump($this->getTest());
  }
}


9.8k
edited Oct '15

Sorry, had no idea you were using it with forms in the UI. In that case, I have no idea. I'm pretty strict about no PHP code in the UI when I build stuff. Are you trying to update a value, dynamically? I guess I'm not understanding your use case. Also, if you're using the variable outside of the class, you'll want to make it public, not protected.



12.6k

In my controller I have 2 actions. In the first action I initialize my variable. This first action is called with Ajax. At the end of the action I send my variable with json encode to the view. After, when I click on another button in my view I'll call the second action with Ajax and in this action I need to get the value of my variable (initialized in the first action) and use it.

I don't wanna use the session. I just want to use a normal utilization of the Object-oriented programming.

I tried already getter / setter but this doesn't work. The variable's value is NULL.



9.8k

I see what's going on... So, the first request is a full request cycle. Variables that are get/set live within that lifecycle. The second button click spawns another request cycle that is totally separate from the first. That's why you're not seeing the variable update.

If you want to use session, you can. You can also use cache. If you use cache, memcached is generally faster than file or DB cache. Just make sure you clean up the session/cached variables or things can get ugly and confusing.

I think that the more simple way to do this is using PHP Globals

$GLOBALS['test'] = 12345;


12.6k

Ok thanks for your answers guys. I hope than there will be a proper way to do this next time.