2012-05-18 208 views
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`,但仍然收到相同的消息。 任何帮助表示赞赏。

+0

我觉得在图书馆名称中使用骆驼情况codeigniter.So CmmonFunct不支持应该被重新命名为Common_funct .Isn,t it – jayadevkv

回答

10

如果库扩展CI_Controller。这意味着您实际上正在扩展控制器的功能。相反,声明你的图书馆这样的:

class CommonFuncts { } 

你并不需要从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 
+1

如果你想使用SIMPLER的方式,那么你必须传递3个参数:'$ this-> load-> library('CommonFuncts',null,'common');' – Mike

+0

你是对的,尽管根据文档它应该是一个空字符串('''')而不是'null'。 –

4

你很困惑controllerslibraries

自定义库不应该扩展CI_Controller。 The documentation outlines how to create your own custom library

其次,你需要加载库,然后才能访问它的方法:

$this->load->library('CommonFuncts'); 
// The "CommonFuncts" library is now ready to use 
$this->commonFuncts->verificaCookie(); 
+0

解决!看起来我在第23行打错了,而不是使用'$ this-> commonfuncts-> verificaCookie()'我正在使用'$ this-> common ** F ** unts-> verificaCookie()'。不管怎么说,还是要谢谢你 ! –

+0

@ LuisM.Valenzuela我很高兴你的工作。我强烈建议阅读我链接到的文档页面。希望你找到他们有帮助。 –

+0

感谢科林,我已经阅读过它们,但正如你可能猜测的,经过几天的努力才得到正确的结果,我尝试了几乎所有通过互联网阅读的方法,其中之一就是扩展了CI_Controller ... –

0

在这里,我要修改一件事(我正在使用代码点火器2.1。3):

 
//echo CI_VERSION; to get the code igniter version 
Plesae use 
$this->commonfuncts->verificaCookie(); //f is small here commonfuncts 
instead of 
$this->commonFuncts->verificaCookie();
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'); 

    } 
}