【问题标题】:PHP- debug shown, but function doesn't fully execute, no errors in console显示 PHP- 调试,但函数未完全执行,控制台中没有错误
【发布时间】:2018-09-18 09:51:47
【问题描述】:

我是 PHP 和 HTTP 的新手,在尝试调试函数时遇到了一个似乎有点奇怪的问题:

有一个页面显示交易列表(历史交易和待处理交易)。信息显示在一个表格中,每个交易都有一行。每笔交易都会显示客户姓名、ID、金额、交易联系人、临时联系人、状态等信息

用户可以通过从“交易联系人”列的预填充下拉列表中选择名称来更新交易联系人。如果他们想在该下拉列表中添加新名称,他们可以单击临时联系人列表中单元格中的编辑按钮,这将打开一个对话框,其中显示一个包含“名字”、“姓氏”和'电子邮件地址'。添加这些详细信息后,他们可以单击表单上的“添加用户”按钮,新用户将被添加到该单元格中的联系人列表中。此单元格中的所有用户都将显示在该交易的“交易联系人”单元格的下拉列表中(以及一些其他默认联系人)。

但是,当前存在一个问题,即当用户将新联系人添加到临时联系人单元格时,尽管该用户随后显示在“事务联系人”下拉列表中,但当您选择新用户时,会显示错误说明用户不存在。但是,如果您在将联系人添加到临时联系人单元格后刷新页面,然后尝试从事务联系人下拉列表中选择它,则可以选择它。

我已将问题缩小到在 UserController.php 中定义的 addAccountUser() 函数中的某处:

public function addAccountUser( AddAccountUsersRequest $request )
{
  dd('addAccountUser() being called ');
  //dd($request);
  $users = $request->input('users');
  $type = $request->input('type');
  $accountId = $request->input('accountId');
  dd('Value of users: ');
  dd($type);

      // If adding an agent, the new user should be agent, else the created user will be direct
      $userType = $type == 'taxfirm-agent' ? UserType::where('userTypeTag', 'AGENT')->first() : UserType::where('userTypeTag', 'DIRECT')->first();
      $messages = array();
      $hasWarningMessages = false;

      try
      {
          DB::beginTransaction();

        foreach ($users as $userRaw)
        {

              $details = array(
                  'firstName' => $userRaw['firstName'],
                  'lastName'  => $userRaw['lastName'],
                  'email'     => $userRaw['email'],
                  'password'  => uniqid(),
                  'userTypeId' => $userType->userTypeId,
                  'accountId' => (!empty($accountId)) ? $accountId : null
              );
        $propertyValues = array();

        // Adding tax agent
        if ($type == 'taxfirm-agent') {
                  $group = $userRaw['role'];
          $rv = $this->addTaxfirmAgent($details, $group);
        }
        else if($type == 'taxfirm-direct') {
          $rv = $this->addTaxfirmDirectContact($details);
        }
        else {
                  $group = $userRaw['role'];
          $rv = $this->addTaxpayerDirectContact($details, $group);
        }

            DB::commit();

        if ($rv['status'] !== 'SUCCESS') {
            if (!isset($messages[$rv['status']])) {
              $messages[$rv['status']] = array(
                'message' => StatusMessage::getMessage($rv['status']),
                'data' => [],
              );
            }

            $messages[$rv['status']]['data'][] = [$userRaw['email'], ucfirst($userRaw['firstName']), ucfirst($userRaw['lastName'])];

            if (!$hasWarningMessages)
            {
              $hasWarningMessages = true;
            }
        }
        }
    }
      catch(\Exception $e)
      {
          DB::rollback();
    return response()->json(array(
      'success' => false,
        'exceptionCode' => $e->getCode(),
        'exceptionMessage' => $e->getMessage().' - '.$e->getFile().' - '.$e->getLine()
    ), 400);
      }

      // When returning $messages to angular, the array is turned into an object
      // which is not desirable. This is a workaround so that the messages are returned
      // as an array
      $outputMsg = array();
      foreach ($messages as $value) {
          $outputMsg[] = $value;
      }

  return response()->json(array(
    'success' => true,
    'hasWarningMessages' => $hasWarningMessages,
          'result' => $outputMsg,
          'users' => $rv['user']->user,
  ));
}

目前,当我尝试将新用户添加到临时联系人单元格以进行交易时,浏览器中会显示一条错误消息:

添加您的用户时出错。如果问题仍然存在,请联系我们。

此时我可以在我的浏览器控制台的 Network -> Preview 选项卡中从 addAccountUser() 函数的开头看到 addAccountUser() being called 调试,但看不到 Value of users:$type 调试我已添加,这表明以下三行之一出现问题:

$users = $request->input('users');
$type = $request->input('type');
$accountId = $request->input('accountId');

我不完全理解这三行中发生了什么...我知道它们是 HTTP 请求,但并不真正知道它们请求的是什么信息,或者从哪里...

谁能指出我解决这个问题的正确方向?

【问题讨论】:

  • 首先,删除所有dd()-calls,因为(如果您使用 Laravel)这些函数调用将转储值,然后完全终止脚本的执行。
  • 嗯,好的。是的,我正在使用 Laravel——如何在不杀死脚本的情况下调试它?
  • 我习惯使用 JavaScript console.log() 语句在 web 控制台中调试东西,但不知道 PHP 的等价物...
  • 所以,当我删除dd() 调用时,用户已成功添加,我可以在事务联系人的下拉列表中看到它,但是当我尝试选择它时,我得到一个在浏览器窗口中显示Unexpected token < in JSON at position 0 的消息(即在地址栏下方弹出 - 这不是控制台中的消息)。

标签: php http httprequest


【解决方案1】:

当您调用dd() 函数时,脚本将输出给函数的任何内容,然后退出。因此,它不会执行以下任何代码。

所以,比如:

dd('Value of users: ');
dd($type);

永远不会工作,因为第二个dd() 永远不会被执行,因为脚本在第一个dd() 之后退出。

PHP 是服务器端,因此您必须在后端渲染一些内容并在浏览器中显示它。 PHP 无法写入浏览器控制台。我将如何进行调试,删除第一个 dd() 调用 (dd('addAccountUser() being called ');) 并查看是否可以到达以下 dd() 调用。

如果这是您浏览器中的发布请求,您可以在开发者控制台 (F12) 中检查该请求并查看来自后端的响应。如果是 Value of users: 之类的,那么您知道可以拨打第二个 dd() 电话。

此外,在控制器中保留dd() 将始终向前端返回错误代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多