- 下载CI框架并解压到工作目录
- 配置nginx,并重启
nginx -s reload
server {
charset utf-8;
client_max_body_size 128M;
listen 80;
server_name ci-with-orm.com;
root /Users/clive/Workspace/ci_with_orm;
index index.php index.html;
access_log /data/access.log main;
error_log /data/error.log;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
location ~* /. {
deny all;
}
}
- 修改本地hosts配置
# 新增一条
127.0.0.1 ci-with-orm.com
- 此时已经能访问ci主页
http://ci-with-orm.com/
默认主页
- 接下来开始接入Laravel的ORM模块,修改composer.json,"require"下新增一行
"illuminate/database": "^5.4"
{
"description": "The CodeIgniter framework",
"name": "codeigniter/framework",
"type": "project",
"homepage": "https://codeigniter.com",
"license": "MIT",
"support": {
"forum": "http://forum.codeigniter.com/",
"wiki": "https://github.com/bcit-ci/CodeIgniter/wiki",
"slack": "https://codeigniterchat.slack.com",
"source": "https://github.com/bcit-ci/CodeIgniter"
},
"require": {
"php": ">=5.3.7",
"illuminate/database": "^5.4"
},
"suggest": {
"paragonie/random_compat": "Provides better randomness in PHP 5.x"
},
"require-dev": {
"mikey179/vfsStream": "1.1.*",
"phpunit/phpunit": "4.* || 5.*"
}
}
- 项目根目录下,执行
composer install
,会生成vendor目录,ORM模块也包含在内 - 修改
application/config/autoload.php
// 注释该行
// $autoload['libraries'] = array();
// 新增该行
$autoload['libraries'] = array('database');
- 修改
application/config/config.php
// 注释该行
// $config['composer_autoload'] = FALSE;
// 新增该行
$config['composer_autoload'] = realpath(APPPATH . '../vendor/autoload.php');
- 修改
application/config/database.php
//注释以下内容
//$active_group = 'default';
//$query_builder = TRUE;
//
//$db['default'] = array(
// 'dsn' => '',
// 'hostname' => 'localhost',
// 'username' => '',
// 'password' => '',
// 'database' => '',
// 'dbdriver' => 'mysqli',
// 'dbprefix' => '',
// 'pconnect' => FALSE,
// 'db_debug' => (ENVIRONMENT !== 'production'),
// '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 IlluminateDatabaseCapsuleManager as Capsule;
$active_group = 'default';
$query_builder = TRUE;
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => 'psw',
'database' => 'test',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => $db['default']['hostname'],
'database' => $db['default']['database'],
'username' => $db['default']['username'],
'password' => $db['default']['password'],
'charset' => $db['default']['char_set'],
'collation' => $db['default']['dbcollat'],
'prefix' => $db['default']['dbprefix'],
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
- 新增模型
application/models/Order.php
<?php
use IlluminateDatabaseEloquentModel as Eloquent;
# 确保数据库中有orders表
class Order extends Eloquent{
protected $table = 'orders';
}
- 修改
application/controllers/Welcome.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
var_dump($this->getOrders());
exit(0);
$this->load->view('welcome_message');
}
public function getOrders() {
$this->load->model("Order");
$orders = Order::all()->toArray();
return $orders;
}
}
- 再次访问测试地址
http://ci-with-orm.com/
就可以通过ORM查询到orders表中的数据了。
orders数据