一、添加database支持。在kohana\application\bootstrap.php下找到如下段
Kohana::modules(array(
// 'auth' => MODPATH.'auth', // Basic authentication
// 'cache' => MODPATH.'cache', // Caching with multiple backends
// 'codebench' => MODPATH.'codebench', // Benchmarking tool
// 'database' => MODPATH.'database', // Database access
// 'image' => MODPATH.'image', // Image manipulation
// 'orm' => MODPATH.'orm', // Object Relationship Mapping
// 'unittest' => MODPATH.'unittest', // Unit testing
// 'userguide' => MODPATH.'userguide', // User guide and API documentation
));
去掉database前面的注释。
二、在kohana\application\config下添加database.conf配置文件,内容如下
return array
(
'default' => array
(
'type' => 'mysql',
'connection' => array(
'hostname' => 'localhost',
'username' => 'root',
'password' => 'password',
'persistent' => FALSE,
'database' => 'kohanademo',
),
'table_prefix' => '',
'charset' => 'utf8',
'profiling' => TRUE,
),
);
?>
修改对应的数据库名和密码就好。
三、在代码中新家数据库查询段,kohana\application\classes\controller\user\user.php内容修改如下
class Controller_User_User extends Controller {
public function action_index()
{
$username = Session::instance()->get('username');
$this->response->body('logined:'.$username);
}
public function action_login()
{
if($this->request->post())
{
$username = $this->request->post('username');
$password = $this->request->post('password');
$query = DB::query(Database::SELECT, "SELECT username FROM user WHERE username=:username AND password=:password");
$query->param(':username', $username);
$query->param(':password', $password);
$user = $query->execute()->current();
if(count($user) > 0)
{
Session::instance()->set('username',$username);
}
$this->request->redirect('/user/user/index');
}
}
}
?>
这样就可以完成数据查询用户信息登录的功能了。