3

我正在尝试构建一个自定义库,其中包含可通过整个站点使用的功能。在里面/application/libraries我做了一个新文件CustomFuncts

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * @brief CommonFuncts      
 * This class is used to provide common user functions to the whole application.
 *
 * @version 1.0
 * @date May 2012
 *
 * 
 */
class CommonFuncts extends CI_Controller {

 protected $ci;

/**
 * function constructor
 */
function __construct()
{
    $this->ci =& get_instance();
}
 /**
* @brief checkCookie
* 
* Checks for a previous cookie, and if exists:
* @li Loads user details to CI Session object.
* @li Redirects to the corresponding page.
* 
*/
public function verificaCookie(){
    $this->ci->load->view('index');
}
 }
/* End of file CommonFuncts.php */
/* Location: ./application/libraries/CommonFuncts.php */

在控制器中welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

/**
 * Index Page for this controller.
 *
 * Maps to the following URL
 *      http://example.com/index.php/welcome
 *  - or -  
 *      http://example.com/index.php/welcome/index
 *  - or -
 * Since this controller is set as the default controller in 
 * config/routes.php, it's displayed at http://example.com/
 *
 * So any other public methods not prefixed with an underscore will
 * map to /index.php/welcome/<method_name>
 * @see http://codeigniter.com/user_guide/general/urls.html
 */
public function index()
{
    $this->load->library('commonFuncts');
    $this->commonFuncts->verificaCookie();  
}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */

我收到以下错误消息:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$commonFuncts

Filename: controllers/welcome.php

Line Number: 23

Fatal error: Call to a member function verificaCookie() on a non-object in
/var/www/vhosts/unikodf.com/httpdocs/application/controllers/welcome.php on line 23

我尝试了很多东西,包括在库中扩展CI_Controller和使用$this->load->view' instead of$this->ci->load->view`,但我仍然收到相同的消息。

4

5 回答 5

10

如果您的库扩展CI_Controller. 这意味着您实际上是在扩展控制器的功能。而是像这样声明您的库:

class CommonFuncts { }

您不需要继承 from CI_Controller,因为您正在编写库而不是控制器,并且库不是 MVC 模型的一部分。它是扩展框架功能、修复常见问题或具有许多控制器使用的功能的单个类。

要访问它的方法,请使用:

$this->load->library('CommonFuncts');
$this->commonfuncts->verificaCookie();

如果要简化调用库的名称,请使用:

// The second argument is the optional configuration passed to the library
// The third argument is the name with which you would like to access it
$this->load->library('CommonFuncts', '', 'common');
$this->common->verificaCookie(); //  /\ = configuration
//     /\/\/\ = name
于 2012-05-19T06:11:28.977 回答
4

您将controllerslibrary混淆了。

自定义库不应扩展 CI_Controller。该文档概述了如何创建您自己的自定义库

其次,您需要先加载库,然后才能访问其方法:

$this->load->library('CommonFuncts');
// The "CommonFuncts" library is now ready to use
$this->commonFuncts->verificaCookie(); 
于 2012-05-18T16:57:21.303 回答
0

这里我想修改一件事(我使用的是代码点火器 2.1.3):

//回显 CI_VERSION; 获取代码点火器版本
请使用
$this->commonfuncts->verificaCookie(); //f 在这里比较小
代替
$this->commonFuncts->verificaCookie();
于 2014-11-16T13:26:39.437 回答
0

您的构造函数不正确。你用过:

$this->ci =& get_instance();  //This is invalid syntax. 

分配codeIgniter给一个变量并在 codeigniter 的资源上使用该变量

class exampleClass{
    protected $codeIgniter;

    public function __construct()
    {
        $this->codeIgniter =& get_instance();
        $this->codeIgniter->load->helper('url');
        $this->codeIgniter->config->item('base_url');

    }
}
于 2017-05-31T15:02:46.850 回答