2012-10-27 3 views
1

5 개의 데이터베이스가있는 database.php 파일이 있습니다.CI 2에서 모든 데이터베이스의 가용성을 확인하는 방법

데이터베이스 중 하나를 사용할 수없는 경우 모든 페이지에서 "사이트를 사용할 수 없습니다"라는 메시지와 함께 500 오류를 얻으려면 어떻게해야합니까?

+0

그래서 하나의 데이터베이스로도 연결할 수없는 경우 사이트에 대한 액세스를 종료하겠습니까? – Brendan

답변

1

나는 당신의 질문을 매우 흥미로 웠고 문제를 해결하기 위해 연구를 해왔다. 난 당신에게 내 솔루션을 말해 : 첫 번째는이 변화 할 당신의 config.php 파일에서, 그래서 후크를 활성화하는 것입니다

$config['enable_hooks'] = TRUE; 

일단 후크를 활성화를, 당신은 그것을 위해, 새로운 후크를 작성해야 다음과 같은 것을 넣어 hooks.php/설정 파일 :

$hook['post_controller_constructor'] = array(
    'class' => 'DBTest', 
    'function' => 'index', 
    'filename' => 'dbtest.php', 
    'filepath' => 'hooks', 
    'params' => array(), 
); 

따라서, 컨트롤러가 인스턴스화되고 있지만, 아직 방법을 실행하지되면 귀하의 후크 차기. 이것은 $의 CI를 사용하는 neccesary입니다 = & get_instance()

은 다음과 같은 내용으로 파일 /application/hooks/dbtest.php 작성을 완료하려면 다음

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class DBTest { 

    function index() { 

    $CI = &get_instance(); 

    $databases = array(
     'mysqli://user1:[email protected]/db1', 
     'mysqli://user2:[email protected]/db2', 
     'mysqli://user3:[email protected]/db3', 
     'mysqli://user4:[email protected]/db4', 
     'mysqli://user5:[email protected]/db5', 
    ); 

    foreach ($databases as $dsn) { 

     $db_name = substr(strrchr($dsn, '/'), 1); 
     $CI->load->database($dsn); 
     $CI->load->dbutil(); 

     if(!$CI->dbutil->database_exists($db_name)) { 
     // if connection details incorrect show error 
     show_error("Site is not available: can't connect to database $db_name");     
     } 
    } 

    } 
} 

당신은 $ CI에 대한 DSN을 사용해야합니다 -> load-> database() 이렇게하면 데이터베이스를로드하려고 할 때 Code Igniter 대신 오류를 처리 할 수 ​​있습니다.

희망이 도움이됩니다.

관련 문제