如何将变量从一种方法传递到另一个内部控制器

时间:2019-05-30 11:15:15

标签: php codeigniter model-view-controller methods

我想将所有这些变量放在一个地方,以便我可以使用模型将它们添加到DB中。 预先谢谢你。

          public function web_login()
    {
            $data['user'] = array();

            if ($this->facebook->is_authenticated())
            {

                $user = $this->facebook->request('get', '/me?fields=id,name,email');
                if (!isset($user['error']))
                {
                    $data['user'] = $user; // ***WANNA PASS THIS VARIABLE FROM HERE***

                    $this->load->view('user/header');
                    $this->load->view('user/fbpass');
                    $this->load->view('user/footer');

            }
            else {
                $this->load->view('user/register');
            }
        }
}

第二种方法________________________________________________

                public function getpass(){
                $this->load->model('user_model', 'auth');
                $this->load->view('user/header');
                $this->load->library('form_validation');
                $this->form_validation->set_rules('password', 'Password', 'required');
                $this->form_validation->set_rules('passwordagain', 'Password Confirmation', 'required|matches[password]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('user/fbpass');
                }
                else  
                {
                        $password=$this->input->post('password');

                       ***//WANNA GET THAT VARIABLE HERE***
                }            

        }

2 个答案:

答案 0 :(得分:0)

class ClassName{
    public $var;
    function one(){
        $this->var = 'value';
    }

    function two(){
        return $this->var; // if you call function one then call function two you will get value
    }
}

但是如果您需要在不同时间打电话给他们,则可以使用会话

$_SESSION['var'] = 'value';

答案 1 :(得分:0)

class TestClass extends Controller {
    function test_mehtod($param) {
        //do something
       echo $param."<br>";
       echo "hi, you are in same controller";
    }

    function test_mehtod2() {
        //do something-else
        $this->test_mehtod("param value");
        // and we called the other method here!
    }
}