Codeigniter AutoLoad DB,但有能力检查连接是否存在 [英] Codeigniter AutoLoad DB, But have ability to check if connection exists

查看:136
本文介绍了Codeigniter AutoLoad DB,但有能力检查连接是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用autoload.php自动加载数据库



我已经得到了自动加载...这很容易。



但是也允许我检查数据库是否已经连接到,如果不是,显示一个特定的页面,说我们很抱歉,但我们的数据库目前不可用。


解决方案

是否必须是某种自定义钩子?

您可以在 application / errors / error_db.php 中编辑错误模板,以使用您的自定义消息。也许可以使用 ENVIRONMENT 常量,并执行这样的操作:

  ?php if(ENVIRONMENT ==='production'):?> 
< p>很抱歉,我们的数据库目前无法使用。< / p>
<?php else:?>
<! - 具有实际数据库错误消息的默认模板 - >
<?php endif; >

否则,我想你可以使用钩子。如果您要自动加载数据库类并希望拦截错误,则必须至少 pre_controller ,或者您可以在<$ c $中加载数据库类在进行连接检查后,可以使用 MY_Controller 类的c> __ construct()你甚至可以直接在 index.php 的第一行做。



但是:我认为你正在寻找可能在语言文件。请查看:
system / language / english / db_lang.php

如果您在同一位置创建自己的文件,在 / application 而不是 / system ,您可以重写相应的语言行:

  // $ lang ['db_unable_to_connect'] ='无法使用提供的设置连接到数据库服务器。 
$ lang ['db_unable_to_connect'] ='很抱歉,但我们的数据库目前不可用。

...只需编辑错误模板中的CSS和/或标记。再次,您可以利用 ENVIRONMENT 常数在开发模式下显示有用的错误,并在生产中显示用户友好的错误。



当然,如果你不能连接到你的数据库,这是一个需要立即注意的主要问题。


How would I autoload the database using autoload.php

I already got the autoloading of course... that's the easy part.

But also allowing me to check if the db has been connected to, and if not, to display a specific page saying something like "We are sorry but our database is currently unavailable."

Would it have to be some kind of custom hook that I would have to make?

解决方案

You can edit the error template in application/errors/error_db.php to use your custom message. Maybe use the ENVIRONMENT constant and do something like this:

<?php if (ENVIRONMENT === 'production'): ?>
  <p>We are sorry but our database is currently unavailable.</p>
<?php else: ?>
  <!-- default template with actual database error messages -->
<?php endif; ?>

Otherwise, I suppose you could use a hook. It would have to be at least pre_controller if you are auto loading the database class and want to intercept the error, or you could just load the database class in the __construct() of a MY_Controller class after doing your connection check. You could even just do it right in the first line of index.php if you wanted to.

However: I think what you're looking for might be in the language files. Take a look at:
system/language/english/db_lang.php.
If you create your own file in the same place, but in /application instead of /system, you can just rewrite the appropriate language line(s):

// $lang['db_unable_to_connect'] = 'Unable to connect to your database server using the provided settings.';
$lang['db_unable_to_connect'] = 'We are sorry but our database is currently unavailable.';

...and just edit the CSS and/or markup in the error template. Once again, you can take advantage of the ENVIRONMENT constant to show useful errors in development mode, and show user-friendly ones in production.

Of course, if you can't connect to your database - that's a major problem that needs immediate attention.

这篇关于Codeigniter AutoLoad DB,但有能力检查连接是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆