php - 具有数据库事务的 Laravel Controller-Model 异常处理结构

标签 php laravel exception-handling transactions try-catch

关于架构,当从模型向 Controller 抛出异常时,两者中哪一个是好的做法?

结构A:

用户 Controller .php

public function updateUserInfo(UserInfoRequest $request, UserModel $userModel)
{
    $isError = false;
    $message = 'Success';

    try {
        $message = $userModel->updateUserInfo($request->only(['username', 'password']));
    } catch (SomeCustomException $e) {
        $isError = true;
        $message = $e->getMessage();
    }

    return json_encode([
        'isError' => $isError,
        'message' => $message
    ]);
}

用户模型.php

public function updateUserInfo($request)
{
    $isError = false;
    $message = 'Success';

    $username = $request['username'];
    $password = $request['password'];

    try {
        $this->connect()->beginTransaction();

        $this->connect()->table('users')->where('username', $username)->update(['password' => $password]);

        $this->connect()->commit();
    } catch (\Exception $e) {
        $this->connect()->rollback();
        $isError = true;
        $message = $e->getMessage();        
    }

    return [
        'isError' => $isError,
        'message' => $message
    ];
}

结构 B:

用户 Controller .php

public function updateUserInfo(UserInfoRequest $request, UserModel $userModel)
{
    $isError = false;
    $message = 'Success';

    try {
        $userModel->updateUserInfo($request->only(['username', 'password']));
    } catch (SomeCustomException $e) {
        $isError = true;
        $message = $e->getMessage();
    } catch (QueryException $e) {
        $isError = true;
        $message = $e->getMessage();    
    }

    return json_encode([
        'isError' => $isError,
        'message' => $message
    ]);
}

用户模型.php

public function updateUserInfo($request)
{
    $username = $request['username'];
    $password = $request['password'];

    try {
        $this->connect()->beginTransaction();

        $this->connect()->table('users')->where('username', $username)->update(['password' => $password]);

        $this->connect()->commit();
    } catch (\Exception $e) {
        $this->connect()->rollback();
        throw new QueryException();
    }
}

结构A中,模型捕获任何异常,回滚事务并在有错误或无错误时返回给 Controller 。然后 Controller 只返回从模型返回的任何内容。

虽然在结构 B 中模型捕获任何异常,但如果发生异常,则回滚事务然后抛出 QueryException。然后 Controller 从模型中捕获抛出的 QueryException,如果有错误或没有错误则返回。

Structure B 仍然有问题的原因是该模型应该是执行回滚的模型。如果我在这里删除模型上的 try-catch 和 Controller 以直接捕获异常,那么回滚将在 Controller 上处理,我认为这有点困惑 Controller 的功能。

让我知道你的想法。 谢谢!

最佳答案

为什么我认为 B 的方法更好:

  1. 您的模型应该只包括逻辑部分:这包括与数据库的通信(事务和回滚),而不是您要打印给用户的错误消息的格式。

  2. 保持模型干净:它是 MVC 结构中最重要的部分。如果你搞砸了,将很难找到任何错误。

  3. 外包错误处理:如果你把它放在 Controller 中,你可以选择在那里处理它(也许你想要这个方法的一些特殊格式的输出,或者你需要调用一些其他函数)或者你处理它在 App\Exceptions\Handler 中。在这种情况下,您可以在此处呈现此错误消息,而不必在 Controller 中进行。

因此,如果您不需要任何特殊的函数调用并且想使用 Laravel 的全部功能,我建议您使用 Structure C

用户 Controller .php

public function updateUserInfo(UserInfoRequest $request, UserModel $userModel)
{
    $userModel->updateUserInfo($request->only(['username', 'password']));
    return response()->json(['message' => 'updated user.']); 
}

用户模型.php

public function updateUserInfo($request)
{
    $username = $request['username'];
    $password = $request['password'];
    try {
        $this->connect()->beginTransaction();

        $this->connect()->table('users')->where('username', $username)->update(['password' => $password]);

        $this->connect()->commit();
    } catch (\Exception $e) {
        $this->connect()->rollback();
        throw new QueryException();
    }
}

应用\异常\处理程序

public function render($request, Exception $exception)
{
    //catch everything what you want
    if ($exception instanceof CustomException) {
        return response()->json([
          'message' => $exception->getMessage()
        ], 422);
    }

    return parent::render($request, $exception);
}

您可以清楚地分离数据库内容(模型)、表示内容( Controller )和错误处理(处理程序)。结构 C 允许您在另一个 Controller 函数中遇到相同情况的其他函数中重用错误处理。

这是我的观点,但我愿意讨论您认为这种方法不是最佳解决方案的任何情况。

关于php - 具有数据库事务的 Laravel Controller-Model 异常处理结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43863392/

相关文章:

php - 这是在 Oracle 数据库中 INSERT 行的正确方法吗?

php - 在 Laravel 中使用表单请求时如何检查验证是否失败?

haskell - 为什么捕获异常是非纯的,而抛出异常是纯的?

Laravel 5.0- Blade 模板错误

php - 在php mysql中创建多维关联数组

c++ - Xcode/LLVM catch 子句不匹配派生类型

.net - .Net Windows 服务中的 UnhandledException 处理程序

php - mysql数据库中没有记录时间

PHP - 如何获取 n 深多维数组的所有数组值?

javascript - 从 JS 向 PHP 传递值