使用 Codeigniter 搭建简易论坛系统 (一)

今天继续深入学习 Codeigniter 这个框架,尝试着用它搭一个简易的论坛系统,熟悉一下操作。
大致讲一下每个页面的 MVC 构成。

项目地址:
https://github.com/Robinson28years/CodeIgniter-learning

查看所有帖子

模型层

1
2
3
4
5
6
7
public function get_threads($id = FALSE)
{
if ($id === FALSE) {
$query = $this->db->get('threads');
return $query->result_array();
}
}

控制器层

1
2
3
4
5
6
7
8
9
10
public function index()
{
$data['threads'] = $this->forum_model->get_threads();
$data['title'] = '论坛首页';

$this->load->view('templates/header', $data);
$this->load->view('forum/index',$data);
$this->load->view('templates/footer');
}

视图层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class="container">
<div class="row">
<a href="<?php echo site_url('forum/create') ?>" class="btn btn-success" type="button" >创建帖子</a>
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><?php echo $title ?></div>

<div class="panel-body">
<?php foreach ($threads as $thread): ?>
<article>
<a href="<?php echo site_url('forum/view/'.$thread['id']);?>">
<h4><?php echo $thread['title']; ?></h4>
</a>
<div class="body"><?php echo $thread['body']; ?></div>
</article>
<hr>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>

效果图

新建帖子

因为视图层代码太长,所以接下来就不放视图层代码了,有需要的评论里说下。

模型层

1
2
3
4
5
6
7
8
9
10
public function set_threads()
{
$data = array(
'user_id' => 1,
'title' => $this->input->post('title'),
'body' => $this->input->post('body')
);

return $this->db->insert('threads', $data);
}

控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public function create()
{


$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');

$data['title'] = '创建帖子';

if ($this->form_validation->run() === FALSE) {
$this->load->view('templates/header',$data);
$this->load->view('forum/create',$data);
$this->load->view('templates/footer');
}
else {
$this->forum_model->set_threads();
redirect('/forum');
}
}

效果图

查看单个帖子

模型层

1
2
3
4
5
6
7
8
9
10
public function get_threads($id = FALSE)
{
$query = $this->db->select('threads.id, threads.title, threads.body, users.name')
->from('threads')
->from('users')
->where('threads.id', $id)
->where('users.id = threads.user_id')
->get();
return $query->row_array();
}

控制层

1
2
3
4
5
6
7
8
9
10
11
12
public function view($id)
{


$data['thread'] = $this->forum_model->get_threads($id);
$data['replies'] = $this->forum_model->get_replies($id);
$data['title'] = '讨论';

$this->load->view('templates/header', $data);
$this->load->view('forum/view',$data);
$this->load->view('templates/footer');
}

效果图

修改帖子内容

模型层

1
2
3
4
5
6
7
8
9
10
11
public function update_threads($id)
{
$data = array(
'id' => $id,
'user_id' => 1,
'title' => $this->input->post('title'),
'body' => $this->input->post('body')
);

$this->db->replace('threads', $data);
}

控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public function update($id)
{


$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');

$data['title'] = '更新帖子';

if ($this->form_validation->run() === FALSE) {
$data['thread'] = $this->forum_model->get_threads($id);
$this->load->view('templates/header',$data);
$this->load->view('forum/update',$data);
$this->load->view('templates/footer');
}
else {
$this->forum_model->update_threads($id);
redirect('/forum/view/'.$id);
}
}

效果图

回复帖子

模型层

1
2
3
4
5
6
7
8
9
10
public function set_replies($id)
{
$data = array(
'user_id' => 1,
'thread_id' => $id,
'body' => $this->input->post('body')
);

return $this->db->insert('replies', $data);
}

控制器层

1
2
3
4
5
6
7
8
public function reply($id)
{
$this->form_validation->set_rules('body', 'Body', 'required');
if ($this->form_validation->run() === true) {
$this->forum_model->set_replies($id);
redirect('/forum/view/'.$id);
}
}

效果图

最后

论坛系统和购物系统是学习一个语言最好的实践,今天看着手册简单的实现了一下基本的论坛功能,但是还没做论坛用户权限功能,明天的工作就是将用户权限实现一下,做好拦截。