codeigniter 在代码点火器3上使用雄辩

rks48beu  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(187)

发布Codeigniter 3后,从这个tutorial我有一些问题
我想使用Eloquent并执行以下步骤
1.安装合成器
1.使用此json文件安装依赖项

{
      "require": {
      "illuminate/database": "4.2.6"
 },

"autoload": {
"classmap": [
 "application/core",
"application/models",
"application/libraries"
]
},

"config": {
"vendor-dir": "vendor/"
}
}

1.安装成功
1.下面进行一些配置:
a.更新autoload.php => $autoload['libraries'] = array('database');上的库
B.更新config.php => $config['composer_autoload'] = TRUE;
c.更新数据库. php

$active_group = 'default';
  $query_builder = TRUE;

  $db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'ci3',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
  );
  use Illuminate\Database\Capsule\Manager as Capsule;

  $capsule = new Capsule;

  $capsule->addConnection(array(
      'driver' => in_array($db['default']['dbdriver'], array('mysql', 'mysqli')) ? 'mysql' : $db['default']['dbdriver'],
      'host' => $db['default']['hostname'],
      'database' => $db['default']['database'],
      'username' => $db['default']['username'],
      'password' => $db['default']['password'],
      'charset' => 'utf8',
      'collation' => 'utf8_unicode_ci',
      'prefix' => $db['default']['dbprefix'],
    )
  );

  $capsule->setAsGlobal();
  $capsule->bootEloquent();

  $events = new Illuminate\Events\Dispatcher;
  $events->listen('illuminate.query', function($query, $bindings, $time, $name) {

    // Format binding data for sql insertion

    foreach ($bindings as $i => $binding) {
      if ($binding instanceof \DateTime)  {
        $bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
      } else if (is_string($binding)) {
        $bindings[$i] = "'$binding'";
      }
    }

    // Insert bindings into query
    $query = str_replace(array('%', '?'), array('%%', '%s'), $query);
    $query = vsprintf($query, $bindings);

    // Add it into CodeIgniter
    $db =& get_instance()->db;
    $db->query_times[] = $time;
    $db->queries[] = $query;
  });

  $capsule->setEventDispatcher($events);

d.创建一个名为ci3的数据库,并使用表users代替它
这是我的模特

<?php

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

use \Illuminate\Database\Eloquent\Model as Eloquent;

class Users extends Eloquent {
    protected $table = "users";
}

和我的控制器

<?php

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

  class User extends CI_Controller {

      public function __construct() {
          parent::__construct();
          $this->load->model('users');
      }

      public function index($page = 1) {
          $users = Users::all();
          foreach ($users as $user) {
              echo '<li>' . $user->username . '</li>';
              echo '<li>' . $user->password . '</li>';
              echo '<li>' . $user->email . '</li>';
          }
          echo '</ul>';
      }

  }

这是我的错误报告

致命错误:在第88行的C:\xampp\htdocs\ci3\应用程序\配置\数据库. php中没有找到类“照明\数据库\胶囊\管理器”

遇到PHP错误
严重性:错误
消息:未找到类“照明\数据库\胶囊\管理器”
文件名:config/functions.php
行号:88
回溯:
我不知道如何把胶囊类

luaexgnf

luaexgnf1#

这里是问题...你错过了'事件'库从laravel,添加下面的行到您的composer.json:
"illuminate/events": "5.0.28"
在我的例子中,我使用了Laravel 5,所以,我的composer.json看起来像:

{
        "require": {
                "illuminate/database": "5.0.28",
                "illuminate/events": "5.0.28"
        } 
}

请检查我是如何实现here的。

nwwlzxa7

nwwlzxa72#

您对文件application/config.php做了哪些更改?

$config['composer_autoload'] = 'path/to/vendor/autoload.php';
pkbketx9

pkbketx93#

我用以下方法解决了这个问题:

composer install

看起来composer update不够,并得到相同的错误。
我仍然不知道如何链接到可能重复的问题(Message: Class 'Illuminate\Database\Capsule\Manager' not found in Codeigniter3.1),希望这对面临同样错误的人有所帮助。

相关问题