简体   繁体   English

cakephp:访问同一个控制器但功能不同的变量

[英]cakephp: access a variable in same controller but different function

i wish to access a variable in cakephp which is global in scope - that is it is in same controller but different function. 我希望在cakephp中访问一个全局范围内的变量 - 也就是说它在同一个控制器中但功能不同。 code: 码:

homeController.php 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? 如何在不使用会话的情况下实现此目的?

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
    }
...
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); 
   }
}

在beforeFilter()中设置变量,您可以在控制器中的任何位置使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM