详细介绍如何使用CodeIgniter搭建博客系统 - $data['
CodeIgniter是一个流行的PHP框架,用于快速开发Web应用程序。以下是使用CodeIgniter搭建博客系统的步骤:
安装CodeIgniter:首先,确保你已经安装了PHP和MySQL。然后,通过Composer安装CodeIgniter。在命令行中运行以下命令:
composer require codeigniter/framework
myblog
php -S localhost:8080 -t myblog
config/database.php
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'your_username';
$db['default']['password'] = 'your_password';
$db['default']['database'] = 'myblog_db';
$db['default']['dbdriver'] = 'mysqli';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on' => false,
$db['default']['cachedir' => '',
$db['default']['char_set' => 'utf8',
$db['default']['dbcollat' => 'utf8_general_ci']);
application/modelsUser.phpUser
class User extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_all_users() {
$this->db->select('*');
$this->db->from('users');
$query = $this->db->get();
return $query->result();
}
}
application/controllersBlogController.phpBlog
class BlogController extends CI_Controller {
public function index() {
$data['title'] = 'My Blog';
$data['posts'] = $this->user_model->get_all_users();
$this->load->view('templates/header', $data);
$this->load->view('blog/index', $data);
$this->load->view('templates/footer'); 拼多多帮助力入口
}
}
application/views/templates/header.phpfooter.phpheader.php
<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
</head>
<body>
<nav>
<ul>
<li><a href="">Home</a></li>
<li><a href="">About</a></li>
<li><a href="">Contact</a></li>
</ul>
</nav>
<!-- Your blog content goes here -->
</body>
</html>
运行和测试:最后,运行以下命令启动你的博客系统:
php -S localhost:8080 -t myblog
http://localhost:8080/myblog