zoukankan      html  css  js  c++  java
  • CodeIgniter整理

    控制类文件都放在application/controllers/文件夹下面
    类名要和类文件名相同(类名开头大写,文件开头可以小写)
    控制类要继承CI_Controller类
    事例
    application/controllers/news.php文件:
    <?php
    class News extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
            $this->load->model('news_model');//引入某个数据库控制类
        }
    
        public function index()
        {
            $data['news'] = $this->news_model->get_news();//$this->数据库控制类名->方法名();数据库控制类名开头小写
            $data['title'] = 'News archive';//模板文件内可以直接诶通过 $title 来获取值
    
            $this->load->view('templates/header', $data);//调用application/views/文件夹下的指定路径的某个模板
            $this->load->view('news/index', $data);
            $this->load->view('templates/footer');
        }
    
        public function view($slug)
        {
            $data['news_item'] = $this->news_model->get_news($slug);
    
            if (empty($data['news_item']))
            {
                show_404();
            }
    
            $data['title'] = $data['news_item']['title'];
    
            $this->load->view('templates/header', $data);
            $this->load->view('news/view', $data);
            $this->load->view('templates/footer');
        }
    }
    ?>
    
    
    
    
    
    
    CodeIgniter的模板一般就是php文件,并且模板内直接使用php语言,不推荐使用模板语言
    模板文件放在application/views/文件夹内
    在application/views/文件夹内可以根据所属不同的控制类把模板文件再分类到不同文件夹内,如可以把News类的模板放在 application/views/news/ 文件夹内
    事例
    application/views/news/index.php文件:
    <?php foreach ($news as $news_item): ?>
    
    <h2><?php echo $news_item['title'] ?></h2>
    <div id="main">
            <?php echo $news_item['text'] ?>
    </div>
    <p><a href="http://[你的域名]/index.php/news/<?php echo $news_item['slug'] ?>">View article</a></p>
    
    <?php endforeach;?>
    
    
    
    
    路由
    http://localhost/CodeIgniter/index.php/news/index
    访问规则
    http://localhost/CodeIgniter/index.php/后面第一部分为指定哪个控制器,第二部分为该控制器的哪个方法,其余部分为该方法的参数,方法可以有多个参数,这里也可以写多个部分
    
    
    
    
    
    数据库
    在application/config/database.php文件修改连接数据库的参数
    
    hostname - 数据库的主机名,通常位于本机,可以表示为 "localhost".
    username - 需要连接到数据库的用户名.
    password - 登陆数据库的密码.
    database - 你需要连接的数据库名.
    dbdriver - 数据库类型。如:mysql、postgres、odbc 等。必须为小写字母。
    dbprefix - 当运行Active Record查询时数据表的前缀,它允许在一个数据库上安装多个CodeIgniter程序.
    pconnect - TRUE/FALSE (boolean) - 使用持续连接.
    db_debug - TRUE/FALSE (boolean) - 显示数据库错误信息.
    cache_on - TRUE/FALSE (boolean) - 数据库查询缓存是否开启,详情请见数据库缓存类。
    cachedir - 数据库查询缓存目录所在的服务器绝对路径。
    char_set - 与数据库通信时所使用的字符集。
    dbcollat - 与数据库通信时所使用的字符规则。
    swap_pre - A default table prefix that should be swapped with dbprefix. This is useful for distributed applications where you might run manually written queries, and need the prefix to still be customizable by the end user.
    autoinit - Whether or not to automatically connect to the database when the library loads. If set to false, the connection will take place prior to executing the first query.
    stricton - TRUE/FALSE (boolean) - 是否强制使用 "Strict Mode" 连接, 在开发程序时,使用 strict SQL 是一个好习惯。
    如果要设置端口号可以添加一项 $db['default']['port'] = 3306;
    
    
    数据库控制类文件放在application/models文件夹内
    命名规则 表名_model ,文件名和类名相同,文件名开头小写,类名开头大写
    数据库控制类要继承CI_Model类,并且要明确调用父类构造函数
    事例
    application/models/news_model.php文件:
    <?php
    class News_model extends CI_Model {
    
        public function __construct()
        {
            $this->load->database();
        }
        public function get_news($slug = FALSE)
        {
            if ($slug === FALSE)
            {
                $query = $this->db->get('news');
                return $query->result_array();
            }else{
                $query = $this->db->get_where('news', array('slug' => $slug));
                return $query->row_array();
            }
        }
    }
    ?>
  • 相关阅读:
    python eval lmbda
    python函数--day14-03
    深浅拷贝--day14-02
    数据结构与算法--排序
    数据结构与算法--栈(stack)与队列(queue)
    完全背包的计数问题
    [题解] Codeforces Round #568 (Div. 2) C题题解
    [题解] Codeforces Round #640 (Div. 4) C题 题解
    数据结构——程序设计(一)单链表功能的操作与实现
    [题解] Codeforces Round #708 (Div. 2) C1 题解报告
  • 原文地址:https://www.cnblogs.com/dreamhome/p/2858878.html
Copyright © 2011-2022 走看看