在null错误CodeIgniter上调用成员函数

时间:2015-05-17 20:10:30

标签: php codeigniter locate

请不要问为什么我有多个控制器:这是我老师想要的。 我有3个控制器: Api.php Token.php Apikey.php ,其中Api.php是“主要”控制器。< / p>

问题:当我转到网址时:http://localhost/revolution/index.php/api/registerUser/first_name/12First

Api 控制器加载令牌控制器似乎存在问题。

错误:致命错误:在null上调用成员函数generateToken()

我该怎么办?

API控制器: Api.php

class api extends CI_Controller {


    public function index()
    {

        parent::__construct();
        $this->load->library("../controllers/Apikey.php");
        $this->load->library("../controllers/Token.php");

    }

    public function registerUser ($username,$parola)
    {
        if ($this->isValidUserName($username) && $this->isValidPass($parola)) {
            $this->load->library("../controllers/Token");
            $id = $this->ApiModel->insertCredentials($username, md5($parola));
            $this->Token->generateToken($id);
            $this->ApiKey->generateApiKey($id);

            $data['registered'] = 1;
            $this->load->view('api', $data);
        }
    }
}

控制器 Token.php

 public function existsToken($token)
    {
        $arrayTokens = $this->ApiModel->getAllTokens();

        if (in_array($token, $arrayTokens))
            return existsToken(sha1($this->randomString())); //$this->isValidToken(sha1($this->randomString()));

        return $token;
    }

    public function randomString() {
        return intval(993432422 % rand());
    }

   public function generateToken($id_user)
    {
        $token = $this->existsToken(sha1($this->randomString()));       
        $date = $this->generateExpDate();
        $result = $this->ApiModel->insertToken($token, $date, $id_user);    
    }

1 个答案:

答案 0 :(得分:-1)

由于codeignetor遵循MVC模式,因此无法在其他控制器中调用控制器函数。但是,您可以使用函数帮助程序执行此操作,并在任何控制器中从函数帮助程序调用该函数。