前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >搭建CI使用Laravel ORM模块

搭建CI使用Laravel ORM模块

作者头像
Clive
发布2019-05-31 10:11:48
1.1K0
发布2019-05-31 10:11:48
举报
文章被收录于专栏:Clive的技术分享
  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 Illuminate\Database\Capsule\Manager 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 \Illuminate\Database\Eloquent\Model 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数据

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019.05.27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档