話説甲大濕手持一把桃木劍,不慌不忙從袋子裏抽出一張符文,只見上面寫着CodeIgniter幾個大字。
眾鄉親不解,甲大濕搖搖頭,拿過旁邊大媽手裏的拖把,去旁邊的河岸裏沾了點水,在地面上洋洋灑灑地寫上 今天宜寫博客 幾個大字。眾鄉親恍然大悟,紛紛拍掌歡呼。
咳咳咳,進入今天的正題,閒着無聊,看看機油用ci寫的博客,心裏癢癢,於是看着ci文檔,小菜鳥按照自己理解,寫了個十分簡單的博客系統,就只有登錄註冊,curd文章的功能。
來次狗!
首先我們要註冊,註冊怎麼寫呢,不就是一個表單提交,然後插入數據庫的菊花嘛(咦),等等,註冊?我們是不是需要一個user的model呢?
// modelsuser_model.php
load->database();
}
// user裏我們需要創建用户,就4行數據
public function create_user() {
$data = array(
'username' => $this->input->post('username'),
'nickname' => $this->input->post('nickname'),
'password' => $this->input->post('password'),
'email' => $this->input->post('email')
);
return $this->db->insert('users',$data);
}
}
// controllersuser.php
class User extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function register() {
$this->load->helper('form');
$this->load->library('form_validation');
// 這裏做表單驗證,就是上面的form_validation提供的一些函數,大家可以通過函數名看出這些函數是做什麼的,等等,第一個參數是表單的name,第二個是等下提示錯誤信息用的暱稱,第三個就是處理方式
$this->form_validation->set_rules('username','Username','trim|required|xss_clean|is_unique[users.username]');
$this->form_validation->set_rules('password','Password','trim|required|mathes[passconf]|md5');
$this->form_validation->set_rules('passconf','Password Confirmation','required');
$this->form_validation->set_rules('email','Email','required|is_unique[users.email]');
if($this->form_validation->run() === FALSE) {
$data['title'] = "Register";
$this->load->view('templates/header',$data);
$this->load->view('user/register');
$this->load->view('templates/footer');
} else {
// 如果通過驗證,那麼創建用户
$this->user_model->create_user();
redirect('/login');
}
}
}
到這裏,我們就能實現一個簡單的註冊功能,等等,什麼?你説你報錯? 對了,忘記説了,這位客官有沒有發現,config文件夾有一個叫autoload的東西,就是用來自動給我們跑腿的。比如
$autoload['libraries'] = array('database','session');
$autoload['helper'] = array('url');
$autoload['model'] = array('user_model');
控制器和模型都寫完了,那就可以叫出前台mm了
<!doctype html>
<html lang="zh-cmn-hans">
<head>
<title></title>
</head>
<body><!--viewstemplatesfooter.php-->
* * *
<strong>© 2014 By fakefish</strong>
</body>
</html>
<h2>Register</h2>
<input type="text" name="username" placeholder="username" value="<?php echo set_value('username'); ?>">
<br>
<input type="text" name="nickname" placeholder="nickname" value="<?php echo set_value('nickname'); ?>">
<br>
<input type="password" name="password" placeholder="password" value="<?php echo set_value('password'); ?>">
<br>
<input type="password" name="passconf" placeholder="confirm password" value="<?php echo set_value('passconf'); ?>">
<br>
<input type="email" name="email" placeholder="email" value="<?php echo set_value('email'); ?>">
<br>
<input type="submit" name="submit" value="Register">
</form>
<a href="./login">Login</a>
等等,這個set_value()是什麼玩意兒,這個就是提交不符合規則頁面跳轉之後,自動會把之前的值填進來。
註冊完成之後,我們就要用登陸了
// controllersuser.php
public function login() {
$this->load->helper('form');
$this->load->helper('url');
$this->load->library('form_validation');
if($this->session->userdata('logged_in')){
redirect('/');
}
// 這裏要判斷是否已經登錄
$this->form_validation->set_rules('username','Username','trim|required|xss_clean');
$this->form_validation->set_rules('password','Password','md5');
$data['title'] = "Login";
if($this->form_validation->run() === FALSE) {
$this->load->view('templates/header',$data);
$this->load->view('user/login');
$this->load->view('templates/footer');
} else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->user_model->get_user($username);
if($result && $password == $result->password) {
$this->load->view('user/login-success');
$this->session->set_userdata('uid',$result->id);
$this->session->set_userdata('username',$result->username);
$this->session->set_userdata('nickname',$result->nickname);
$this->session->set_userdata('email',$result->email);
$this->session->set_userdata('logged_in',TRUE);
redirect('/');
} else {
$this->load->view('templates/header',$data);
$this->load->view('user/login');
$this->load->view('templates/footer');
}
}
}
其實如果這位客官看到這裏,應該能拓展着寫完剩下的開發。
代碼不就細給了,講一些自己碰到的一些東西。
1.數據庫連接查詢
public function get_list_post($p = FALSE) {
$this->db->select('*');
$this->db->from('posts');
$this->db->join('users','users.id = posts.uid');
$this->db->order_by('pid desc');
if($p === FALSE) {
$this->db->limit(5);
} else {
$this->db->limit($p*5+5,$p*5);
}
$query = $this->db->get();
return $query->result();
}
這裏,由於我設計的posts裏有一列是uid對應users裏的id,每次查文章要把兩個並起來查,然後被這個limit()坑了,上面寫的是對的,但是我一開始寫的是反的,我感覺反着寫才更符合人的想法啊。
整個博客的源碼我放在github上了,在線demo
其實作為一個渣前端,沒怎麼學過服務器方面的知識,比如默認的路由是隻能是/index.php/write,怎麼辦呢,本地是apache,就修改.htaccess文件咯
RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /ciblog/index.php/$1 [L]