Codeigniter 3应用程序:如果数据库中没有表,则重定向到某个控制器
I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.
I use migration files (001_create_authors.php
up to 005_create_comments.php
) to automatically create the necessary database tables.
The controller running the migrations is at
application/controllers/Migrate.php
It has the flowing 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 {
echo 'Migration executed';
}
}
}
The default controller is the Posts
controller, as the routes.php
file shows: $route['default_controller'] = 'posts';
I would like the Posts
controller to redirect to the Migrate
one, if there are no tables in the database. Does Codeigniter have a method to determine if there are no tables? How shall I use it?
我正在使用 Codeigniter 3.1.8 strong>和开发基本博客应用程序 Bootstrap 4 strong>。 p>
我使用迁移文件( 运行迁移的控制器位于 p>
application / controllers / Migrate.php p>
blockquote >
它有流动的代码: p>
默认控制器是 我希望 001_create_authors.php code>至
005_create_comments.php code>)自动创建 必要的数据库表。 p>
class Migrate extends CI_Controller
{
public function __construct()
{
parent :: __construct();
}
公共函数索引()
{
$ this-> load-> library('migration');
if($ this-> migration- > current()=== FALSE)
{
show_error($ this-> migration-> error_string());
}
else {
echo'Transse executed';
} \ n}
}
code> pre>
Posts code>控制器,作为
routes.php code>文件 显示:
$ route ['default_controller'] ='posts'; code> p>
帖子 code>控制器重定向到迁移 code>一,如果数据库中没有表 em>。 Codeigniter是否有方法来确定是否没有表格? 我该如何使用它? p>
div>
https://www.codeigniter.com/user_guide/database/metadata.html
if (count($this->db->list_tables()) == 0) {
redirect(...);
}
I got the desired result this exact way:
In the Posts controller (default):
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
}
In the Migrate controller:
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('/');
}
}
}