调用null错误CodeIgniter成员函数 [英] Call to a member function on null error CodeIgniter

查看:225
本文介绍了调用null错误CodeIgniter成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要问为什么我有多个控制器:这是我的老师想要的。
我有3个控制器: Api.php Token.php Apikey.php ,其中Api.php是main :当我传入 URL localhost / revolution / index.php / api / registerUser / first_name / 12首先rel =nofollow> http://localhost/revolution/index.php/api/registerUser/first_name/12第一



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



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



我能做什么?



API控制器: Api.php

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

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


解决方案

在CodeIgniter中加载库

 函数__construct()
在控制器的构造函数中, {
//构造我们的父类
parent :: __ construct();
$ this-> load-> library('apikey');
$ this-> load-> library('token');
}

请尝试移除您的索引 p>

并将 Apikey.php和Token.php 移动到CodeIgniter的libraries文件夹。要进一步了解如何在CodeIgniter项目中创建和使用PHP类作为库,请阅读以下链接:


  1. CodeIgniter文档

  2. StackOverflow答案

希望此回答对您有所帮助!



干杯!


Please dont`t ask why I have more than one controller: this is what my teacher wants. I have 3 controllers: Api.php, Token.php and Apikey.php, where Api.php is the "main" one.

PROBLEM: When I pass into the URL: http://localhost/revolution/index.php/api/registerUser/first_name/12First

it seems there are problems with loading the Token controller from Api controller.

ERROR: Fatal error: Call to a member function generateToken() on null

What can I do?

API Controller: 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);
        }
    }
}

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

解决方案

Loading libraries in CodeIgniter is done as follows within the Constructor of the controller not inside an Index function written in the Controller.

function __construct()
{
    // Construct our parent class
    parent::__construct();
    $this->load->library('apikey');
    $this->load->library('token');
}

Try that removing your index function.

And also move your Apikey.php, and Token.php to the CodeIgniter's libraries folder. To further read about creating and using PHP classes as libraries inside CodeIgniter projects please read the following links:

  1. CodeIgniter Docs
  2. StackOverflow Answer

Hope this answer was helpful to you!

Cheers!

这篇关于调用null错误CodeIgniter成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆