久闻php ci的大名,一直没有机会使用,上周做个小项目,用到了这个框架,整体感觉很不错,这里跟大家分享一下ci的快速入门
ci文档入口:http://codeigniter.org.cn/user_guide/toc.html(中文!)
ci,即codeigniter,我用的版本是当前的最新版本2.1.3
使用ci,只要将apache或者nginx的发布目录指到codeigniter的顶级目录下就可以
下面,就按MVC的顺序来所说ci的使用
url
比如要访问/news/list的action,默认情况下,url需要这么来写:xxx.xxx.xxx/index.php/news/list~~多个index.php,很别扭
如果要去掉这个index.php,需要加上“.htaccess”来实现urlRewrite:
1 2 3 | RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] |
C
ci的controller位于application/controllers目录下,文件名和ation方法名需要遵循规约变成的规则
不多说了,还是直接写代码吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class News extends CI_Controller{ function __construct(){ parent::__construct(); } // http://localhost/news/lists,如果要使用其他的url,需要到“application/config/routes.php”去配置路由 public function lists($keyWords){ //使用model,model()方法的第一个参数“news”对应application/models/news.php //第二个参数“News”对应下面如何引用这个model:$this->News,如果不设置这个参数,这是$this->news,及model的名字 $this->load->model('news', 'News'); $data['list1'] = $this->News->list_all(); //使用library核心类 $this->load->library('news_manager'); $data['list2'] = $this->news_manager->search_news($keyWords); //使用view,向浏览器输出内容 $this->load->view('header'); $this->load->view('news/list', $data); $this->load->view('footer'); } // public function view_news($id){ $this->load->view('view_news', array( 'title' => news->title, 'content' => $news->content )); } } |
如果要加载非默认数据源,需要到config/database.php里配置不同的数据源,比如现在除了default,又添加了一个叫做abc的数据源,我们如何调用哪个它哪?
1 2 3 4 5 6 7 8 9 | //abc $abc = $this->load->database('abc', true); $this->load->model('abc_table1'); $thisi->abc_table1->db = $abc; //default $defaultdb = $this->load->database('default', true); $this->load->model('news'); $this->news->db = $defaultdb; //即通过“$this->load->database”建立数据源,然后通过$thisi->model->db=xxx为当前某个model的引用设置数据源 |
另一个要说的功能是:可以将controller文件放在子文件夹中,比如“application/controllers/shop/products.php”,调用它就需要“/index.php/shop/products/xxx”
M
上面的代码已经用到了模型的调用,这里所说模型的常用方法
增
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class news extends CI_Model{ function __construct(){ parent::__construct(); } function cretae($title, $content){ $data = array( 'title' => $title, 'content' => $content, 'create_time' => new time() ); //INSERT INTO news (title, content, create_time) VALUES ('xxxx', 'xxxx', 'xxxx') $this->db->insert('news', $data); } } |
删
1 2 3 4 5 | //DELETE FROM mytable WHERE id = $id $this->db->delete('news', array('id' => $id)); //等效 $this->db->where('id', $id); $this->db->delete('news'); |
改
1 2 3 4 5 6 7 | $data = array( 'title' => $title, 'content' => $content, ); $this->db->where('id', $id); //UPDATE news SET title = xxxx, content = xxx where id = $id $this->db->update('news', $data); |
查
1 2 3 4 5 6 7 8 9 | //SELECT * FROM news $query = $this->db->get('news'); //select * from news where id = $id limit $offset, $limit $query = $this->db->get_where('news', array('id' => $id), $limit, $offset); //直接取结果集 $list = $query->result(); //取结果集的数量 $count = $query->num_rows(); //此外,ci的查询还有丰富的方法,支持各种查询,诸如:where/or_where/where_in/or_where_in/like等等 |
打印sql
1 | echo $this->db->last_query(); |
V
ci的view位于application/views/目录下,比如view_news.php,可以直接使用php代码书写
1 2 3 4 5 6 7 8 | <html> <head> <title><?php echo $title;?></title> </head> <body> <h1><?php echo $content;?></h1> </body> </html> |
上面的title、content参数,可以通过下面的方式在controller中传递过来:
1 2 3 4 | $this->load->view('view_news', array( 'title' => $news->title, 'content' => $news->content )); |
———
参考:http://codeigniter.org.cn/user_guide/toc.html
———
蒋评:
先不论框架的易用性、性能等因素,相对于cakephp和thinkphp,ci最大的又是就是它的中文资料丰富,有完整的中文文档,甚至官网还提供中文视频教程:http://codeigniter.org.cn/tutorials,这些都大大降低了新手的入门难度[赞]