搭建CI使用Laravel ORM模块

2019-05-31 10:11:48 浏览数 (1)

  1. 下载CI框架并解压到工作目录
  2. 配置nginx,并重启nginx -s reload
代码语言:javascript复制
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;
       }
   }
  1. 修改本地hosts配置
代码语言:javascript复制
# 新增一条
127.0.0.1 ci-with-orm.com
  1. 此时已经能访问ci主页 http://ci-with-orm.com/

默认主页

  1. 接下来开始接入Laravel的ORM模块,修改composer.json,"require"下新增一行"illuminate/database": "^5.4"
代码语言:javascript复制
{
    "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.*"
    }
}
  1. 项目根目录下,执行composer install,会生成vendor目录,ORM模块也包含在内
  2. 修改application/config/autoload.php
代码语言:javascript复制
// 注释该行
// $autoload['libraries'] = array();
// 新增该行
$autoload['libraries'] = array('database');
  1. 修改application/config/config.php
代码语言:javascript复制
// 注释该行
// $config['composer_autoload'] = FALSE;
// 新增该行
$config['composer_autoload'] = realpath(APPPATH . '../vendor/autoload.php');
  1. 修改application/config/database.php
代码语言:javascript复制
//注释以下内容
//$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();
  1. 新增模型application/models/Order.php
代码语言:javascript复制
<?php
use IlluminateDatabaseEloquentModel as Eloquent;

# 确保数据库中有orders表
class Order extends Eloquent{
    protected $table = 'orders';

}
  1. 修改application/controllers/Welcome.php
代码语言:javascript复制
<?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;
    }
}
  1. 再次访问测试地址http://ci-with-orm.com/就可以通过ORM查询到orders表中的数据了。

orders数据

0 人点赞