1

i wish to access a variable in cakephp which is global in scope - that is it is in same controller but different function. code:

homeController.php

    public function opauth_complete() {
    //global $facebook_data;
    $this->facebook_data = $this->data;
     debug($this->facebook_data); // returns data
    $this->redirect(array('controller' => 'home', 
                          'action' => 'user_home'));
}
public function user_home()
{
   // i wish to use $facebook_data here.
debug($this->facebook_data); // returns null

}

how do i implement this without using session?

3 Answers 3

2

you can store it in session

class HomeController extends Controller {        

    public function opauth_complete() {
      //assign the value
      $this->Session->write("facebook_data", $this->data);
     debug($this->facebook_data); // returns data
      $this->redirect(array('controller' => 'home', 
                          'action' => 'user_home'));
    }
    public function user_home() {
       //read from session
    debug($this->Session->read("facebook_data")); // returns data
    }
...
Sign up to request clarification or add additional context in comments.

6 Comments

is there a way to do it without session?
@z22 well, i dont think so, because you are redirecting to other function, data in your local variable ($this->facebook_data) will not be available after redirect.
actually i tried it with session also but it still returns null, do i need to define the session helper?
yes you need to include Session Component in your Controller, if not already added in AppController, like using var $components = array('Session');
well i did so, but still it returns null :(, where am i getting wrong?
|
2
App::uses('AppController', 'Controller');

class UsersController extends AppController {

   private $FbData = array();

   public function opauth_complete() {
      $this->FbData = $this->data;
   }

   public function user_home(){
      debug($this->FbData); 
   }
}

Comments

0

Set the variable in beforeFilter(), and You can use everywhere in controller

1 Comment

That would expire... wouldn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.