Php Codeigniter 3应用程序:如果数据库中没有表,则重定向到某个控制器
我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序 我使用迁移文件(Php Codeigniter 3应用程序:如果数据库中没有表,则重定向到某个控制器,php,codeigniter,codeigniter-3,Php,Codeigniter,Codeigniter 3,我正在使用Codeigniter 3.1.8和Bootstrap 4开发一个基本的博客应用程序 我使用迁移文件(001\u create\u authors.php005\u create\u comments.php)自动创建必要的数据库表 运行迁移的控制器位于 application/controllers/Migrate.php 它具有流动代码: class Migrate extends CI_Controller { public function __construct()
001\u create\u authors.php
005\u create\u comments.php)自动创建必要的数据库表
运行迁移的控制器位于
application/controllers/Migrate.php
它具有流动代码:
class Migrate extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('migration');
if($this->migration->current() === FALSE)
{
show_error($this->migration->error_string());
}
else {
echo 'Migration executed';
}
}
}
默认控制器是Posts
控制器,如routes.php
文件所示:$route['default_controller']='Posts'代码>
如果数据库中没有表,我希望Posts
控制器重定向到Migrate
one。Codeigniter是否有确定是否没有表格的方法?我该如何使用它?
我通过这种精确的方式得到了想要的结果:
在Posts控制器中(默认):
在迁移控制器中:
class Migrate extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('migration');
if($this->migration->current() === FALSE)
{
show_error($this->migration->error_string());
}
else {
$this->session->set_flashdata('tables_created', "All the required database tables have been created. You can now register.");
redirect('/');
}
}
}
public function index() {
// Create all the database tables if there are none
// by redirecting to the Migrations controller
if (count($this->db->list_tables()) == 0) {
redirect('migrate');
}
// More code
}
class Migrate extends CI_Controller
{
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->library('migration');
if($this->migration->current() === FALSE)
{
show_error($this->migration->error_string());
}
else {
$this->session->set_flashdata('tables_created', "All the required database tables have been created. You can now register.");
redirect('/');
}
}
}