CI基础

2017-12-15  本文已影响0人  yzw12138

一简介

二CI的URL

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

三控制器

四视图

<html>
<head>
    <title><?php echo $title;?></title>
</head>
<body>
    <h1><?php echo $heading;?></h1>
    <h3>My Todo List</h3>
    <ul>
    <?php foreach ($todo_list as $item):?>
        <li><?php echo $item;?></li>
    <?php endforeach;?>
    </ul>
</body>
</html>
$string = $this->load->view('myfile', '', TRUE);

五模型

 public function blog()
    {
        $this->load->model('blog');
        $data['query'] = $this->blog->get_last_ten_entries();
        $this->load->view('blog', $data);
        //$this->load->model('blog', 'bo');
        //$this->bo->$this->blog->get_last_ten_entries();
    }
$this->load->model('model_name', '', TRUE);

3)如果不想使配置文件中链接,也可以通过第三参数设置数据库链接;

$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = 'mydatabase';
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;

$this->load->model('model_name', '', $config);

六辅助函数

$this->load->helper('name');

加载后可以在控制器和视图中全局访问,加载辅助函数时去掉文件后缀.php和_helper部分;

$this->load->helper(
    array('helper1', 'helper2', 'helper3')
);

七类库使用

$this->load->library('class_name');
$this->load->library(array('email', 'table'));
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
    public function some_method()
    {
         ......
    }
}
$params = array('type' => 'large', 'color' => 'red');
$this->load->library('someclass', $params);

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Someclass {
    public function __construct($params)
    {
        // Do something with $params
    }
}
$CI =& get_instance();

$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');

八驱动器

$this->load->driver('class_name');

通过驱动器类名class_name来加载驱动器

$this->load->driver('some_parent');

通过some_parent调用该类的方法

$this->some_parent->some_method();
$this->some_parent->child_one->some_method();
上一篇 下一篇

猜你喜欢

热点阅读