您好我有问题
假设我在CodeIgniter中有一个文件夹结构
application/
controllers/
models/
views/
gmail_library/
现在我已经写了一个控制器
class invite_friends extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->gmail_library('Config'); // this line is giving me error
session_start();
}
}
我怎样才能设置这样的东西?
答案 0 :(得分:0)
首先,请注意CodeIgniter不会__call()
使用overloading来实现动态方法。因此,没有办法让这种gmail_library()
方法起作用。
来自User Guide:
您的图书馆课程应该放在您的
application/libraries
文件夹,因为这是CodeIgniter的外观 为他们初始化时。
如果您使用CI Loader
类加载库或帮助程序,则应遵循CI的约定。
应用/库/ Myclass.php
$this->load->library('myclass');
$this->myclass->my_method();
1)您将库文件放在主libraries
文件夹中的子目录中:
<子> application/libraries/gmail/Gmail_config.php
我重命名了您的Config.php
文件名,以防止与CI config
核心类发生冲突。
$this->load->library('gmail/gmail_config');
2)您还可以在library
文件夹的Loader::library()
method到load the library file from the outside中使用相对路径,如下所示:
文件的路径是相对的。因此,您可以使用../
在路径中输入 UP 级别
再说一遍:我重命名了您的Config.php
文件名,以防止与CI config
核心类发生冲突。
$this->load->library('../gmail_library/Gmail_config');
答案 1 :(得分:0)
一个老问题,我知道,但是我遇到了这个寻找从应用程序文件夹外部使用类(库)的方法,我喜欢将它保存在“这样做的CI方式”中。我最终扩展了@linkplain
类:
我基本上复制了CI_Loader
函数并添加了绝对路径
_ci_load_class
将文件<? if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
protected $absPath = '/home/xxxxx/[any-path-you-like]/common/';
/**
* Load class
*
* This function loads the requested class.
*
* @param string the item that is being loaded
* @param mixed any additional parameters
* @param string an optional object name
* @return void
*/
public function commonLibrary($class, $params = NULL, $object_name = NULL)
{
// Get the class name, and while we're at it trim any slashes.
// The directory path can be included as part of the class name,
// but we don't want a leading slash
$class = str_replace('.php', '', trim($class, '/'));
// Was the path included with the class name?
// We look for a slash to determine this
$subdir = '';
if (($last_slash = strrpos($class, '/')) !== FALSE)
{
// Extract the path
$subdir = substr($class, 0, $last_slash + 1);
// Get the filename from the path
$class = substr($class, $last_slash + 1);
}
// We'll test for both lowercase and capitalized versions of the file name
foreach (array(ucfirst($class), strtolower($class)) as $class)
{
$subclass = $this->absPath.'libraries/'.$subdir.config_item('subclass_prefix').$class.'.php';
// Is this a class extension request?
if (file_exists($subclass))
{
$baseclass = BASEPATH.'libraries/'.ucfirst($class).'.php';
if ( ! file_exists($baseclass))
{
log_message('error', "Unable to load the requested class: ".$class);
show_error("Unable to load the requested class: ".$class);
}
// Safety: Was the class already loaded by a previous call?
if (in_array($subclass, $this->_ci_loaded_files))
{
// Before we deem this to be a duplicate request, let's see
// if a custom object name is being supplied. If so, we'll
// return a new instance of the object
if ( ! is_null($object_name))
{
$CI =& get_instance();
if ( ! isset($CI->$object_name))
{
return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
}
}
$is_duplicate = TRUE;
log_message('debug', $class." class already loaded. Second attempt ignored.");
return;
}
include_once($baseclass);
include_once($subclass);
$this->_ci_loaded_files[] = $subclass;
return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
}
// Lets search for the requested library file and load it.
$is_duplicate = FALSE;
foreach ($this->_ci_library_paths as $path)
{
$filepath = $this->absPath.'libraries/'.$subdir.$class.'.php';
// Does the file exist? No? Bummer...
if ( ! file_exists($filepath))
{
continue;
}
// Safety: Was the class already loaded by a previous call?
if (in_array($filepath, $this->_ci_loaded_files))
{
// Before we deem this to be a duplicate request, let's see
// if a custom object name is being supplied. If so, we'll
// return a new instance of the object
if ( ! is_null($object_name))
{
$CI =& get_instance();
if ( ! isset($CI->$object_name))
{
return $this->_ci_init_class($class, '', $params, $object_name);
}
}
$is_duplicate = TRUE;
log_message('debug', $class." class already loaded. Second attempt ignored.");
return;
}
include_once($filepath);
$this->_ci_loaded_files[] = $filepath;
return $this->_ci_init_class($class, '', $params, $object_name);
}
} // END FOREACH
// One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
if ($subdir == '')
{
$path = strtolower($class).'/'.$class;
return $this->_ci_load_class($path, $params);
}
// If we got this far we were unable to find the requested class.
// We do not issue errors if the load call failed due to a duplicate request
if ($is_duplicate == FALSE)
{
log_message('error', "Unable to load the requested class: ".$class);
show_error("Unable to load the requested class: ".$class);
}
}
}
放在MY_Loader.php
文件夹中,并使用以下命令加载您的文件:
application/core