【问题标题】:How to pass variables from 1 method to another inside controller如何将变量从一种方法传递到另一个内部控制器
【发布时间】:2019-05-30 11:15:14
【问题描述】:

我想在一个地方获取所有这些变量,以便我可以使用模型将它们添加到数据库中。 提前谢谢你。

          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***
                }            

        }

【问题讨论】:

  • 视情况而定。这两种方法是如何联系起来的?
  • 这些在同一个控制器内
  • 使用 session 存储数据并从 session 获取数据
  • 如果第二种方法首先触发,请在您的代码中更新并在第二种方法中将$user 作为参数传递

标签: php codeigniter methods


【解决方案1】:
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';

【讨论】:

    【解决方案2】:
    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!
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-12
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多