2016-07-20 16 views
0

我不想使用Doctrine等。因此,我只是簡單地使用PDO。問題是我不知道如何處理異常情況:調用$app->abort來顯示它在路由之外不起作用。

<?php 

require_once __DIR__.'/../vendor/autoload.php'; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Request; 

$app = new Silex\Application(); 

//PDO 
try { 
    $DBH = new PDO($PartCCTV_ini['db']['dsn'], $PartCCTV_ini['db']['user'], $PartCCTV_ini['db']['password']); 
} 
catch(PDOException $e) { 
    $app->abort(500, 'PDO Error : '.$e->getMessage());    
} 

... 

$app->run() 

?> 
+0

你期望什麼結果?順便說一句,你爲什麼在應用程序之外做這件事,而不是爲此創建一個服務? –

+0

@dragoste,退出代碼爲500的錯誤頁面和解釋。我想,編寫服務需要很長時間。 – Vasiliy

+0

只需返回像header('HTTP/1.1 500內部服務器錯誤');'然後你也可以發佈任何HTML內容500錯誤頁面。但是你不能在這裏使用Silex應用程序,因爲你在開始之前這樣做。 –

回答

1
//PDO 
try { 
    $DBH = new PDO($PartCCTV_ini['db']['dsn'], $PartCCTV_ini['db']['user'], $PartCCTV_ini['db']['password']); 
} 
catch(PDOException $e) { 
    $Exception = $e->getMessage();  
    $app->before(function() use($Exception) { 
     throw new PDOException($Exception); 
    }); 
} 

「集體農莊式」俄羅斯),但它的作品!