在哪里可以捕获PHP MVC应用程序中的异常? [英] Where to catch exception in PHP MVC application?

查看:139
本文介绍了在哪里可以捕获PHP MVC应用程序中的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个适用于实践OOP和MVC技能的小型/中型PHP应用程序。我有 init / bootstrap 文件调用路由器谁呼叫控制器 - > 服务层 - > 存储库(数据库),然后将变量发送回查看层(所有依赖于DiC / IOC处理)。

I have small/mid sized PHP application built for practicing OOP and MVC skills. I have init/bootstrap file which calls Router who calls Controller -> Service Layer -> Repository (Database) and then sends back variables to View Layer (all dependencies handled with DiC/IOC).

强>抽象类BaseException 扩展Core Exception 类。然后我有不同的异常类 - DatabaseException,FileException等。

I created abstract class BaseException that extends Core Exception class. Then I have different Exception classes - DatabaseException, FileException etc.

示例触发异常: 从数据库获取数据,如果它失败,它会引发新的DatabaseException。

Example triggering Exception: In Database Layer I try to fetch data from database, if it fails it throws new DatabaseException.

示例2: 处理文件包括,保存,删除 - 如果发生错误,它会引发新的FileException。

Example 2: In classes which I handle file include, save, delete - if error occurs it throws new FileException.

在init / bootstrap文件中或在BaseController中放置try catch代码的位置?但是如果Controller失败,它会引发某种ControllerException。

Where to put try catch code, in init/bootstrap file or maybe in BaseController ? But what if Controller fails and it throws some kind of ControllerException.

在我看来,它可能看起来像这样( init.php 文件):

In my opinion it might look like this (init.php file):

try {
    // create new router
    $router = $container->get('Router');
    $router->import'Config/routes.php');

    // run the router
    $router->route();

    // load controller
    $dispatcher = $container->get('Dispatcher');
    $dispatcher->load();
} catch (DatabaseException $e) {
    echo "Database error!";
    $log->log('Database error', $e->getMessage());
} catch (FileException $e) {
    echo "File error!";
    $log->log('File error', $e->getMessage());
} catch (Exception $e) { // default
    echo "Some exceptional error!";
    $log->log('Default exception', $e->getMessage());
}

还有一个问题,如何记录这些例外(错误),就像上面的例子或者我应该在BaseException中注入Log类并处理日志记录吗?

One more question, how to log these exceptions (errors), like example above or should I inject Log class in BaseException and handle logging there ?

推荐答案

如在简单的PHP中,您可以在任何地方实现try / catch您可能需要。

As in plain php, you can implement try/catches everywhere you might need.

在我的项目(作为MVC框架用户)中,我通常处理我的控制器方法中的异常,因为它们有义务处理应用程序中的进程。但是,如果需要,我会在中间件或者模型中实现它,如果我想。

In my projects (as a MVC framework user), I generally handle exceptions in my controllers methods, as they have the duty to handle processes in an application. But if needed, I would implement it in a middleware or even in a model if i want to.

在你的情况下,似乎你想让你的框架处理在输出到用户界面之前的例外,就像Laravel(也是很多人)一样。

In your case, it seems like you want to let your framework handle the exception before outputing it to the user interface, as Laravel (also many others) does.

你可以用 set_exception_handler() code>函数从PHP。

You may accomplish this with set_exception_handler() function from PHP.

这是一个基于 docs 示例:

set_exception_handler(function($exception){

    //you could write in a log file here for example...
    echo "Uncaught exception: " , $exception->getMessage(), "\n";

});

这篇关于在哪里可以捕获PHP MVC应用程序中的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆