·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> [CI]CodeIgniter视图 & 模型 & 控制器

[CI]CodeIgniter视图 & 模型 & 控制器

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
[CI]CodeIgniter视图 & 模型 & 控制器

--------------------------------------------------------------------------------------------------------

载入视图

  $this->load->view('home/name');  //可以用子文件夹存储视图,默认视图文件以'.php'结尾

载入多个视图

  $data['title'] = 'chenwei';      //给视图添加动态数据

  $data['message'] = 'Your message';

  $this->load->view('header', $data); //当一次性载入多个视图时,你只需在第一个视图传入数据即可(header视图显示title, content视图显示message)

  $this->load->view('menu');

  $this->load->view('content');

  $this->load->view('footer');

使用对象的例子:

  $data = new Someclass();

  $this->load->view('blogview', $data);

视图文件中的变量

  <title><?php echo $title; ?></title>

  <div><?php echo $message; ?></div>

创建循环

  class Blog extends CI_Controller{

    function index()

    {

      $data['todo_list'] = array('clean house', 'call mom', 'run errands');

      $data['title'] = 'my real title';

      $data['heading'] = 'my real heading';

      

      $this->load->view('blogview', $data);

    }

  }

  <title><?php echo $title; ?></title>

  <h1><?php echo $heading; ?></h1>

  <ul>

    <?php foreach($todo_list as $item):?>

    <li><?php echo $item; ?></li>

    <?php endforeach; ?>

  </ul>

获取视图内容(赋值给一变量)

  $buffer = $this->load->view('blogview', $data, true);

  //view函数第三个可选参数可以改变函数的行为。如果将view第三个参数设置为true(布尔),则函数返回数据。view函数缺省行为是false,将数据发送到浏览器。如果想返回数据,记得将它赋到一个变量中。

@黑眼诗人:参考用户手册PHP替代语法

视图文件的PHP替代语法 =>

  config/config.php中打开$config['rewrite_short_tags'],那么如果你的服务器不支持短标记,CodeIgniter将重写所有短标记。

  注:如果你使用这个特性,如果在你的视图文件中发生 PHP 错误,则错误信息和行号将无法准确显示。相反,所有的错误将显示为eval ()的错误。

正常的echo形式:<?php echo $variable; ?>

使用替代语法:<?=$variable?>

替代控制结构

  <ul>

    <?php foreach($todo_list as $item): ?>

    <li><?=$item?></li>

    <?php endforeach; ?>

  </ul>

  注:这里没有大括号。相反,结束大括号被替换成了 endforeach 。上面列出的每一个控制结构也有相似的关闭语法:endif, endfor, endforeach 和 endwhile,并且在每个结构以后注意不要使用分号(除了最后一个),用冒号!

  <?php if($username == 'chenwei'): ?>

    <h3>Hi chenwei.</h3>

  <?php elseif($username == 'joe'): ?>

    <h3>Hi Joe</h3>

  <?php else: ?>

    <h3>Hi unknow user</h3>

  <?php endif; ?>

----------------------------------------<www.chenwei.ws>----------------------------------------

模型类文件均存放在 application/models 目录,当然也可以建立子目录,便于大型项目开发管理。

基本的模型类

  1.类名首字母必须大写,其它字母小写,如 '表名_model.php',确保继承基本模型类CI_Model,文件名是模型类名的小写形式。

  2.模型可以在控制器中被引用。

    如:$this->load->model('User_model'); 或 $this->load->model('home/User_model');

   模型一旦被载入 就可以使用,默认情况下模型名称直接被引用作为对象名。

    如:$this->User_model->function();

   当然可以重新命名对象名,通过在加载模型函数中指定第二个参数来设定。

    如:$this->load->model('User_model', 'fubar');

      $this->fubar->function();

自动载入模型

  如果需要特定模型在整个项目中起作用,可以让CI在初始化时自动装载,通过在application/config/autoload.php文件的自动装载数组中添加该模型。

连接到数据库

  模型被载入时不会自动连接数据库,以下方法可以使你连接数据库,

  1.标准方法连接数据库

  2.把第三个参数设置为TRUE来使模型装载函数自动连接数据库

    $this->load->model('User_model', '', TRUE);

  3.手动设定第三个参数来载入你的自定义数据库配置

    $config['hostname'] = 'localhost';

    $config['username'] = 'root';

    $config['passWord'] = 'root';

    $config['database'] = 'test';

    $config['dbdriver'] = 'MySQL';

    $config['dbPRefix'] = '';

    $config['pconnect'] = FALSE;

    $config['db_debug'] = TRUE;

  

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

    //注:自动连接数据库和手动连接数据库时,消耗的内存情况一样。

完整示例:

  class User_model extends CI_Model{

    var $title = '';

    var $connect = '';

    var $data = '';

    function __construct()

    {

      parent::__construct();

    }

    function get_last_ten_entries()

    {

      $query = $this->db->get('entries', 10);

      return $query->result();

    }

    function insert_entry()

    {

      $this->title = $this->input->post('title'); //接收POST提交的数据,使用了input类

      $this->content = $this->input->post('content');

      $this->date = time();

      $this->db->insert('entries', $this);

    }

    function update_entry()

    {   

      $this->title = $this->input->post('title');

      $this->content = $this->input->post('content');

      $this->date = time();

      $this->db->update('entries', $this, array('id'=>$this->input->post('id')));

    }

  }

  //上面用到的函数是 Active Record 数据库函数

-----------------------------------------------------------------------------------------------------

控制器文件一般保存在application/controllers/ 文件夹:

  默认URL路由配置 $config['uri_protocol'] = 'AUTO'; //默认即pathinfo模式,可选

  注:类名必须大写字母开头,首字母小写属于无效写法。

基本的控制器类

  class Blog extends CI_Controller{

    public function __construct()

    {

      parent::__construct();  

      //构造函数并不能返回值,但是可以用来设置一些默认的功能。确保你的控制器扩展自父控制器类,以便它能够继承其所有的方法。

    }

    public function index()

    {

      echo 'Hello World!';

    }

    public function comments()

    {

      $this->load->view('comment');

    }

  }

  //使用 example.com/index.php/blog/comments 来访问 comments方法

定义默认控制器

  application/config/routes.php 中 $route['default_controller'] = 'Blog';

将控制器放入子文件夹

  在application/controllers 目录下新建目录,放入控制器即可。注:如果你要使用某个子文件夹下的功能,就要保证 URI 的第一个片段是用于描述这个文件夹的。application/index.php/home/blog/comments/123

私有方法:

  private function _test()

  {

    return $variable = 'aaa'; //即使不加修饰词private,只要方法名字前带下划线(_)做前缀,即为私有方法,无法通过URL访问。

  }

保留的方法名称:

  控制器类名不能为index, 如 class Index extends CI_Controller{},因为index为CI默认方法名,包含在保留字内,具体参考保留字。

重新定义方法的调用规则:

 _remap();

处理输出:

  _output(); 详细参考输出类。

----------------------------------------------------------------------------------------------