在CodeIgniter中加载外部库Gantii时出错


Error while loading external library Gantii in CodeIgniter

我正试图将其集成到我的PHP项目中:https://github.com/bastianallgeier/gantti

我已经将calendar.php和gantii.php复制到应用程序/库中。

控制器:

<?php
class summary extends CI_Controller {
  function index() {
     $this->load->library('gantti');
     $gantti = new Gantti($data, array(
  'title'      => '',
  'cellwidth'  => 25,
  'cellheight' => 35,
  'today'      => true
));
    $this->load->view('summary_view', $gantti);
  }
}

视图:

<?php
require('lib/gantti.php'); 
require('controllers/summary.php'); 
date_default_timezone_set('UTC');
setlocale(LC_ALL, 'en_US');
?>
<!DOCTYPE html>
<html>
<head>  
  <title>Summary</title>
  <meta charset="utf-8" />  
  <link href="<?php echo base_url(); ?>assets/css/gantti.css" rel="stylesheet" media="screen">
  <link href="<?php echo base_url(); ?>assets/css/metro-bootstrap.css" rel="stylesheet" media="screen">
  <script type="text/javascript" src="<?php echo base_url(); ?>assets/js/jquery-1.10.2.js"></script>
</head>
<body>
<div class="page-header">
  <h1>Summary</h1>
</div>
<?php echo $gantti ?>
</body>
</html>

但是我没有查看页面,而是收到如下错误(404):

A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Gantti::__construct(), called in C:'xampp'htdocs'wwww'system'core'Loader.php on line 1099 and defined
Filename: libraries/gantti.php
Line Number: 18
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: libraries/gantti.php
Line Number: 29

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: libraries/gantti.php
Line Number: 41
Fatal error: Call to a member function month() on a non-object in C:'xampp'htdocs'www'application'libraries'gantti.php on line 58

load->library`要加载CI中的任何库,它将自动调用其构造函数。与此库中发生的情况相同,但您需要向库传递至少一个参数,这就是它出错的原因,您可以使用CI加载器仅向库传递一个参数

$this->load->library('gantti',$data);

但是你不能设置第二个参数来设置这个库,但是这个库中的所有变量都是公共的,你可以这样调用它们来设置

$this->gantti->options = array(
  'title'      => '',
  'cellwidth'  => 25,
  'cellheight' => 35,
  'today'      => true
)

完整解决方案

$this->load->library('gantti',$data); //first load library and pass data
$this->gantti->options = array(
      'title'      => '',
      'cellwidth'  => 25,
      'cellheight' => 35,
      'today'      => true
    );
$data['gantti'] = $this->gantti->__toString();

另一个解决方案是创建一个自定义库,并使用Gantti库进行扩展,复制库目录下的Gantti-lib目录

自定义库

<?php
    require_once APPPATH.'libraries/lib/gantti.php';
    class cigantti extends Gantti {
        //put your code here
        public function __construct() {
        }
        public function generate($data = array(), $params = array()){
            parent::__construct($data, $params);
            return $this->render();
        }
    }
    ?>

控制器内

$this->load->library('cigantti');
$data['gantti'] = $this->cigantti->generate($data, array(
                    'title' => 'Demo',
                    'cellwidth' => 25,
                    'cellheight' => 35,
                    'today' => true
                ));