代码Igniter自定义库 [英] Code Igniter custom library

查看:165
本文介绍了代码Igniter自定义库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立一个自定义库,它将包含通过整个网站提供的功能。内部 / application / libraries 我创建了一个新文件 CustomFuncts

 <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 
/ **
* @brief CommonFuncts
*此类用于向整个应用程序提供常见的用户函数。
*
* @version 1.0
* @date 2012年5月
*
*
* /
类CommonFuncts扩展CI_Controller {

protected $ ci;

/ **
*函数构造函数
* /
函数__construct()
{
$ this-> ci =& get_instance();
}
/ **
* @brief checkCookie
*
*检查先前的cookie,如果存在:
* @li加载用户详细信息到CI Session对象。
* @li重定向到相应的页面。
*
* /
public function verificaCookie(){
$ this-> ci-> load-> view('index');
}
}
/ *文件结束CommonFuncts.php * /
/ *位置:./application/libraries/CommonFuncts.php * /

并在控制器 welcome.php

 <?php if(!defined('BASEPATH'))exit('不允许直接脚本访问); 

class Welcome extends CI_Controller {

/ **
*此控制器的索引页。
*
*映射到以下URL
* http://example.com/index.php/welcome
* - 或 -
* http:// example.com/index.php/welcome/index
* - 或 -
*由于此控制器在
* config / routes.php中设置为默认控制器,它显示在http: //example.com/
*
*所以任何其他没有以下划线为前缀的公共方法将
*映射到/index.php/welcome/<method_name>
* @see http://codeigniter.com/user_guide/general/urls.html
* /
public function index()
{
$ this-> ; load-> library('commonFuncts');
$ this-> commonFuncts-> verificaCookie();
}
}

/ *文件结束welcome.php * /
/ *位置:./application/controllers/welcome.php * /

我收到以下错误讯息:

 遇到PHP错误

严重性:注意

消息:未定义属性:Welcome :: $ commonFuncts

文件名:controllers / welcome.php

行号:23

致命错误:调用成员函数verificaCookie()对非对象在
/ var / www / vhosts / unikodf.com / httpdocs / application / controllers / welcome.php on line 23

我试过很多事情,包括在库中扩展 CI_Controller 并使用 $ this-> load-> view' / code> $ this-> ci-> load-> view`但仍然得到相同的消息。 如果您的库扩展 CI_Controller c>。这意味着你实际上扩展了控制器的功能。而是像下面这样声明你的库:

  class CommonFuncts {} 

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





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

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

  //第二个参数是传递给库的可选配置
//第三个参数是名称你想要访问它
$ this-> load-> library('CommonFuncts','','common');
$ this-> common-> verificaCookie(); // / \ = configuration
// / \ / \ / \ = name


I'm trying to build a custom library which will contain functions that would be available through the whole site. Inside /application/libraries I've made a new file 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 */

And in controller 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 */

I'm getting the following error message:

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

I've tried many things, including extending in the library the CI_Controllerand using $this->load->view' instead of$this->ci->load->view` but still I'm getting the same message. Any help is appreciated.

解决方案

If your library extends CI_Controller. That means that you are actually extending the functionality of the controllers. Instead declare your library like this:

class CommonFuncts { }

You don't need to inherit from CI_Controller, since you are writing a library not a controller and a library is not part of the MVC model. It's single class which extends the features of the framework, fixes a common issue or has a functionality used by many controllers.

To access it's methods use:

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

If you want to simplify the name with which you call the library use:

// 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

这篇关于代码Igniter自定义库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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