Home > Article > Backend Development > Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial
本文参考 链接 介绍CodeIgniter如何将Controller连接Model层(操作数据库),实现读取新闻条目功能。通过本文串通Controller和Model以及View之间如何协同,控制。 有一点需要明确,关于数据库的操作在model层,而非Controller层,Controller只负责业务控制逻辑,从model里取数据然后送给view。phpmyadmin里: 一,创建Model
CREATE TABLE news (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(128) NOT NULL,
slug varchar(128) NOT NULL,
text text NOT NULL,
PRIMARY KEY (id),
KEY slug (slug)
);<getting started with php framework codeigniter tutorialpre>
新建个表。注意text类型的选utf8编码,然后随便插入两条数据。
<p> <getting started with php framework codeigniter tutorialp>
<p>在models文件夹下新建News_model:<getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<pre class="code">
load->database();
}
public function get_news($slug = FALSE){
if($slug == false){
$query = $this->db->get('news');
return $query->result_array();
}
$query = $this->db->get_where('news', array('slug' => slug));
return $query->row_array();
}
}<getting started with php framework codeigniter tutorialpre>
注意上面result_array()是返回查询到所有的结果,row_array()是返回查询的当前那条结果。关于数据库的部分可以参考 链接
<p> <getting started with php framework codeigniter tutorialp>
<h1>二新建Controller<getting started with php framework codeigniter tutorialh1>
<p>News.php<getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<pre class="code">
load->model(news_model);
$this->load->helper('url_helper');
}
Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial**
* 显示所有新闻
*Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial
public function index(){
$data['news'] = $this->news_model->get_news();
$data['title'] = 'News archive';
$this->load->view('templatesGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialheader', $data);
$this->load->view('newsGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialindex', $data);
$this->load->view('templatesGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialfooter');
}
Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial**
* 显示某一个slug的新闻
* @param null $slug
*Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial
public function view($slug = NULL)
{
$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('templatesGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialheader', $data);
$this->load->view('newsGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialview', $data);
$this->load->view('templatesGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialfooter');
}
}<getting started with php framework codeigniter tutorialpre>
注意:
<p> <getting started with php framework codeigniter tutorialp>
<p>1,Controller如何加载Model?<getting started with php framework codeigniter tutorialp>
<p>在News的构造函数里通过load->model('')里将model目录下对应名字的model加载进来,然后再使用时通过$this->news_model进行调用。<getting started with php framework codeigniter tutorialp>
<p>2,Model的名字是不区分大小写的,即真正的model可以大写,在load的时候可以写成小写。<getting started with php framework codeigniter tutorialp>
<p>3,Controller如何跟view层发生关系?<getting started with php framework codeigniter tutorialp>
<p>通过$this->load->view('')加载view文件夹下的文件,传递一个array。在Controller里的array的key,在view下就是对应的变量名字。关于传递数据这块可以参考 CI的模版解析类部分。<getting started with php framework codeigniter tutorialp>
<p>4,通过代码可以看到,News这个控制器加载了viewGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialnews文件夹下的index.php 和view.php<getting started with php framework codeigniter tutorialp>
<h1>三,新建index.php<getting started with php framework codeigniter tutorialh1>
<p> <getting started with php framework codeigniter tutorialp>
<pre class="code">
<getting started with php framework codeigniter tutorialpre>
<h2> <getting started with php framework codeigniter tutorialh2>
<h3> <getting started with php framework codeigniter tutorialh3>
<div class="main"> <getting started with php framework codeigniter tutorialdiv>
<p>>View article<getting started with php framework codeigniter tutorialp>
注意:此处用了site_url设置超链接,用意是地址栏里输入newsGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialslug能直接跳转到newsGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialviewGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialslug,因此要设置路由。
<p> <getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<p>view.php<getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<pre class="code">
'.$news_item['title'].'';
echo $news_item['text'];
<getting started with php framework codeigniter tutorialpre>
<brgetting started with php framework codeigniter tutorial>
<h1>四,修改routes.php<getting started with php framework codeigniter tutorialh1>
<p> <getting started with php framework codeigniter tutorialp>
<p>在原来基础上,增加以下两句:<getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<pre class="code">
$route['news'] = 'news';
$route['newsGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorial(:any)'] = 'newsGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialviewGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorial$1';<getting started with php framework codeigniter tutorialpre>
<brgetting started with php framework codeigniter tutorial>
<p> <getting started with php framework codeigniter tutorialp>
<h1>五,配置数据库<getting started with php framework codeigniter tutorialh1>
<p>在database.php里配置下数据库相关信息。<getting started with php framework codeigniter tutorialp>
<p>经过以上5步,一切ok。<getting started with php framework codeigniter tutorialp>
<p>浏览器输入:http:Getting Started with PHP Framework CodeIgniter (2)_PHP TutorialGetting Started with PHP Framework CodeIgniter (2)_PHP TutoriallocalhostGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorial~yanziGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialCodeIgniterGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialindex.phpGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialnews<getting started with php framework codeigniter tutorialp>
<p><img alt="Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial" src="http:Getting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20TutorialGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20Tutorialwww.bkjia.comGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20TutorialuploadsGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20TutorialallimgGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20Tutorial151029Getting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20Tutorial041T0G52-0.png" getting started with php framework codeigniter tutorial><getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<p>点击超链接后http:Getting Started with PHP Framework CodeIgniter (2)_PHP TutorialGetting Started with PHP Framework CodeIgniter (2)_PHP TutoriallocalhostGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorial~yanziGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialCodeIgniterGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialindex.phpGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialnewsGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialslug1111 转到以下:<getting started with php framework codeigniter tutorialp>
<p><img alt="Getting Started with PHP Framework CodeIgniter (2)_PHP Tutorial" src="http:Getting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20TutorialGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20Tutorialwww.bkjia.comGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20TutorialuploadsGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20TutorialallimgGetting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20Tutorial151029Getting%20Started%20with%20PHP%20Framework%20CodeIgniter%20(2)_PHP%20Tutorial041T04Q3-1.png" getting started with php framework codeigniter tutorial><getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<p><brgetting started with php framework codeigniter tutorial>
<getting started with php framework codeigniter tutorialp>
<p> <getting started with php framework codeigniter tutorialp>
<getting started with php framework codeigniter tutorialp>
<p align="left"></p>
<div style="display:none;"><span id="url" itemprop="url">http:Getting Started with PHP Framework CodeIgniter (2)_PHP TutorialGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialwww.bkjia.comGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialPHPjcGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorial1065223.html<getting started with php framework codeigniter tutorialspan><span id="indexUrl" itemprop="indexUrl">www.bkjia.com<getting started with php framework codeigniter tutorialspan><span id="isOriginal" itemprop="isOriginal">true<getting started with php framework codeigniter tutorialspan><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http:Getting Started with PHP Framework CodeIgniter (2)_PHP TutorialGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorialwww.bkjia.comGetting Started with PHP Framework CodeIgniter (2)_PHP TutorialPHPjcGetting Started with PHP Framework CodeIgniter (2)_PHP Tutorial1065223.html<getting started with php framework codeigniter tutorialspan><span id="genre" itemprop="genre">TechArticle<getting started with php framework codeigniter tutorialspan><span id="description" itemprop="description">PHP框架CodeIgniter入门使用(2) 本文参考 链接 介绍CodeIgniter如何将Controller连接Model层(操作数据库),实现读取新闻条目功能。通过本文串通...<getting started with php framework codeigniter tutorialspan><getting started with php framework codeigniter tutorialdiv><getting started with php framework codeigniter tutorialp><getting started with php framework codeigniter tutorialdiv>
<div class="art_confoot"><getting started with php framework codeigniter tutorialdiv>
</getting></div></getting></getting></getting></getting></span></getting></span></getting></span></getting></span></getting></span></getting></span></div></getting></getting></p></getting></brgetting></p></getting></p></getting></p></getting></p></getting></p></getting></p></getting></p></getting></p></getting></p></getting>
</h1></getting></p></brgetting></getting>