【问题标题】:Why are my if statements not working properly in Laravel?为什么我的 if 语句在 Laravel 中不能正常工作?
【发布时间】:2021-01-28 19:34:44
【问题描述】:

首先,我进行 API 调用以返回域中的所有电子邮件。我不会费心发布 API 调用,因为它是功能性的。但这就是我对响应所做的:

    $resJson = json_decode($response, TRUE);

    $exclusions = DB::table('exclusion')->get();
    foreach($exclusions as $exclusion) {
      $exc[]=$exclusion->email;
    }

我对它进行 json 解码,然后从数据库中导入一个排除的电子邮件列表,在下面的第一个 if 语句中,我将它与我不希望显示的电子邮件的排除数组进行检查。 p>

然后我附和以下内容。

  <div class="table-responsive">
      <table class="table">
          <thead class="text-primary">
              <th><strong>Users</strong></th><th><strong>Creation Date</strong></th><th><strong>Last Login</strong></th><th><strong>Status</strong></th><th><strong>Suspension Reason</strong></th><th style="text-align: center;"><strong>Action</strong></th>
          </thead>
          <tbody>
            @php
            foreach($resJson["users"] as $user) {
              if(!in_array($user['primaryEmail'], $exc))
              {
                if($user['suspended'] == false)
                {
                  $susEnable = '<form name="suspend" style="text-align: center;" action="/customer/apps/gsuite/suspend" method="POST"><input type="hidden" name="_token" value="'.csrf_token().'"/><input type="hidden" name="email" value="'.$user['primaryEmail'].'"/><button class="btn btn-danger">Suspend Account</button></form>';
                  $status = 'Active';
                  $reason = '';
                }
                elseif($user['suspended'] == true)
                {
                  $susEnable = '<form name="enable" style="text-align: center;" action="/customer/apps/gsuite/restore" method="POST"><input type="hidden" name="_token" value="'.csrf_token().'"/><input type="hidden" name="email2" value="'.$user['primaryEmail'].'"/><button class="btn btn-fill btn-success">Restore Account</button></form>';
                  $status = 'Suspended';
                  $reason = $user['suspensionReason'];
                }
                if($user['lastLoginTime']=="1970-01-01T00:00:00.000Z")
                {
                  $lastLogin = 'Never';
                } else {
                  $lastLogin = str_replace('T',' // ', $user['lastLoginTime']);
                  $lastLogin = str_replace('Z','', $lastLogin);
                }
                $creationTime = str_replace('T',' // ', $user['creationTime']);
                $creationTime = str_replace('Z','', $creationTime);
                    echo '<tr><td style="line-height: 5; color: white;">' . $user['primaryEmail'] . '</td><td style="line-height: 5; color: white;">' . $creationTime . '</td><td style="line-height: 5; color: white;">' . $lastLogin . '</td><td style="color: white;">'.$status.'</td><td style="color: white;">'.$reason.'</td><td>'.$susEnable.'</td></tr>';
                    
              }
            }
            @endphp
          </tbody>
      </table>
  </div>

以上代码取自 Google ADMIN SDK JSON 响应。示例 JSON 响应如下所示:

{
   "kind":"admin#directory#user",
   "id":"115906813143010867543",
   "etag":"",
   "primaryEmail":"amr@domain.com",
   "name":{
      "givenName":"amr",
      "familyName":"h",
      "fullName":"amr h"
   },
   "isAdmin":false,
   "isDelegatedAdmin":false,
   "lastLoginTime":"1970-01-01T00:00:00.000Z",
   "creationTime":"2021-01-11T20:46:25.000Z",
   "agreedToTerms":false,
   "suspended":true,
   "suspensionReason":"ADMIN",
   "archived":false,
   "changePasswordAtNextLogin":true,
   "ipWhitelisted":false,
   "emails":[
      {
         "address":"amr@domain.com",
         "primary":true
      },
      {
         "address":"amr@domain.com.test-google-a.com"
      }
   ],
   "nonEditableAliases":[
      "amr@domain.com.test-google-a.com"
   ],
   "customerId":"C04357r1m",
   "orgUnitPath":"/",
   "isMailboxSetup":true,
   "includeInGlobalAddressList":true,
   "recoveryEmail":""
}

问题是,有时 if 语句会起作用并输出正确的形式,但有时会失败。我看到当正确的按钮确实出现时,它完全按照我的控制器应该做的事情,所以我不会费心在此处发布代码,因为它只是一个 API 调用。

我们以电子邮件为例:

first@domain.com

我在 JSON 响应中看到此电子邮件已被暂停。因此,根据代码中的 if 语句,它应该显示“SUSPENDED”以及暂停原因,以及显示“Restore Account”的 btn-success。但是,它显示的内容与 HTML 中的完全相反。我认为这可能是缓存问题,但事实并非如此。有什么想法吗?

【问题讨论】:

  • 无法在 simplified example 上重现。您将不得不进一步调查这个“有时”方面。
  • 是的。这不是严格的 PHP 问题,if 语句很好。它似乎更像是 Laravel 的“错误”。刚才我发现了这一点:如果我刷新页面几次,它会呈现正确的按钮。但这不是它应该如何运作的,所以它一定是某种缓存问题——不是浏览器缓存问题,而是 Laravel 缓存问题
  • 当你得到错误的结果时,你会检查 html 吗?错误的按钮是否有正确的数据?这意味着它指的是正确的电子邮件。
  • 我检查了,它的数据不正确。我想这是有道理的,因为每次更改视图时都必须运行 php artisan optimize,而这里我是根据 JSON 响应动态更改输出。但这不是实时动态应用程序的解决方案。我想我必须为此找到解决方法。
  • 查看每次尝试之间手动清除缓存是否仍会产生错误(尽管我对此表示怀疑)。我怀疑它不会发生在产品中。

标签: google-api google-admin-sdk


【解决方案1】:

在这上面花了几个小时后,我弄清楚了为什么 if 语句不起作用。该问题实际上与 PHP 或 laravel 完全无关,而是 Google API 本身的问题。

暂停/恢复是同一个API调用不同的参数,而拉取域中的用户数据实际上是另一个API调用。虽然 API 中的暂停和恢复功能是即时的,但第二个 API 需要一两分钟才能真正更新用户的状态。

删除 Laravel/PHP 标记并将 Google 标记添加到此问题中,以防其他人遇到类似问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-14
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2022-12-03
    相关资源
    最近更新 更多