Silex:在中间件中调用$ app-> abort()返回500

时间:2016-07-20 10:55:53

标签: php pdo silex

我不想使用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()

?>

1 个答案:

答案 0 :(得分: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);
    });
}

&#34; Kolkhoz风格&#34; 俄语),但它确实有效!