I do not want to see Yii er开发者_StackOverflow社区ror message when the database connection fails. How can I redirect to a specific page when the database connection fails with Yii framework? Thanks.
To catch all CDbConnection errors you need to enable an error handler in config/main.php
'components'=>array('errorHandler'=>array('errorAction'=>'site/error', ), ),
Then, within your controller (or an abstract base class for all your controllers) you need define an action to accomplish the redirect.
public function actionError() {
if($error=Yii::app()->errorHandler->error)
if ( CDbException == $error->type) {
$this->redirect(array("site/error_message")); }
// call the parent error handler, but something doesn't feel right about this:
else
parent::actionError(); }
Alternatively you can just render your custom views:
public function actionError() {
if($error=Yii::app()->errorHandler->error)
if ( CDbException == $error->type) {
$this->render('error', $error); } }
See Yii docs for more details.
you can do something like:
try {
$connection=new CDbConnection($dsn,$username,$password);
} catch(Exception $e) {
$this->redirect(array('controller/action'));
}
you can also pass additional info with the redirect, see here.
精彩评论