本文介绍了当异常在路由外抛出时,如何在Slim框架中传递错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图围绕着处理Slim框架应用程序抛出的异常以及最终的页面传递的操作顺序。基本上,如果我在一个类中抛出异常,我希望Slim能够提供我漂亮的Twig 500页面,但是当异常被抛出一个路由之前,我甚至不能让Slim提供自己的普通错误页面。 给定此数据库类构造函数: public function __construct $ connection,\Slim\Slim $ slim){ $ this-> slim = $ slim; try { $ this-> db = new \PDO(...); $ this-> db-> setAttribute(\PDO :: ATTR_EMULATE_PREPARES,FALSE); $ this-> db-> setAttribute(\PDO :: ATTR_ERRMODE,\PDO :: ERRMODE_EXCEPTION); } catch(\PDOException $ e){ //如何在这里杀死进一步的执行,并且Slim提供500? } } 如果我运行 $ this - > slim-> error(); 我得到致命错误:未捕获异常Slim\Exception\Stop'。 理想情况下,我想执行以下操作: 记录通过 $ this-slim-> log->错误(无法连接到数据库); 停止尝试在我的DB类中执行进一步的操作(将全部失败并导致致命的异常) 通过一个500.twig的模板文件传送页面。 任何帮助或方向都将不胜感激。解决方案由于Slim的错误处理尚未配置,因为您的应用程序从未将其一直运行到 \Slim\Slim :: run()。 要做的两件事: 1)我建议将您的数据库类(和其他类似的类)添加到Slim的依赖注入容器。 $ app-> container-> singleton('db',function()use($ app){ retu新数据库($ app); }); 这将允许延迟加载数据库连接。在使用它之前,不会创建该类。在这一点上,我假设在这里,你将在路由中, \Slim\Slim :: run()将已经被调用,并且Slim错误处理将到位。 2)现在在应用程序完全配置之前不会发生异常,您可以使用日志记录: public function __construct(\Slim\Slim $ app){ $ this-> slim = $ app; try { $ this-> db = new \PDO('sqlite:/ does / not / exists'); } catch(\PDOException $ p){ $ this-> slim-> log-> error('BAD THINGS'); return $ this-> slim-> error(); } return $ this; } 以及Slim的自定义错误处理。 $ app-> error(function(\\ \\ Exception $ e)use($ app){ if($ e instanceof \PDOException){ return $ app-> render('500.twig',array(),500); } }); I'm trying to wrap my head around the order of operations for dealing with Exceptions thrown in a Slim framework app and the final page delivery. Basically, if I throw an Exception in a class I'd like Slim to deliver my pretty Twig 500 page, but I can't even get Slim to deliver its own normal error page when an exception is thrown outside of a route.Given this database class constructor:public function __construct(array $connection, \Slim\Slim $slim) { $this->slim = $slim; try { $this->db = new \PDO(...); $this->db->setAttribute(\PDO::ATTR_EMULATE_PREPARES, FALSE); $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); } catch (\PDOException $e) { // How can I kill further execution here and have Slim deliver a 500? }}If I run $this->slim->error(); I get Fatal error: Uncaught exception 'Slim\Exception\Stop'.Ideally, I'd like to do something like:Log it via $this-slim->log->error("Unable to connect to database.");Stop attempting to execute further actions in my DB class (which would all fail and throw fatal exception)Deliver the page via a 500.twig template file.Any help or direction would be much appreciated. 解决方案 You're running into grief because Slim's error handling hasn't been configured because your app never makes it all the way to \Slim\Slim::run().Two things to do:1) I recommend adding your database class (and other similar classes) into Slim's Dependency Injection container.$app->container->singleton('db', function () use ($app) { return new Database($app);});That will allow lazy loading of your database connection. The class won't be created until you use it. At that point, and I'm assuming here, you will be in a route, \Slim\Slim::run() will have been called, and the Slim error handling will be in place.2) Now that the exception isn't going to occur before your app is fully configured, you can use logging:public function __construct(\Slim\Slim $app) { $this->slim = $app; try { $this->db = new \PDO('sqlite:/does/not/exist'); } catch (\PDOException $p) { $this->slim->log->error('BAD THINGS'); return $this->slim->error(); } return $this;}along with Slim's custom error handling.$app->error(function(\Exception $e) use ($app) { if ($e instanceof \PDOException) { return $app->render('500.twig', array(), 500); }}); 这篇关于当异常在路由外抛出时,如何在Slim框架中传递错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 13:37