从回调返回值

时间:2018-07-28 16:28:53

标签: php callback

注册新用户后,我收到selectortoken用于帐户验证。 我想知道是否发送了确认邮件,但是从回调返回值时遇到了麻烦。这是我所拥有的:

  try {
      $callback = function ($selector, $token) {
          $msg = "some message";
          if(mail($_POST['email'],"Please verify your account",$msg))
          {
              return "success";
          }
          else
          {
              return "mail_not_sent";
          }
      };
      $auth->registerWithUniqueUsername($_POST['email'], $_POST['password'], $_POST['username'], $callback);
      $output['result']=$callback; //this is the array where I want to store the result in ("success" of "mail_not_sent").
  }
  catch ($e) {
  }

2 个答案:

答案 0 :(得分:0)

It doesn't look like Auth::registerWithUniqueUsername() gives you access to the result of the callback,所以如果我必须这样做,我会做这样的事情:

$callback_result = '';

$callback = function ($selector, $token) use (&$callback_result) {
    /* Other code here */
    $callback_result = 'whatever';
    /* Other code here */
};

$auth->registerWithUniqueUsername(/* Other args here */, $callback);
$output['result'] = $callback_result;

答案 1 :(得分:0)

这有点奇怪,但是:

       try {
      $mailFlag = null;
      $callback = function ($selector, $token) use (&$mailFlag) {
          $msg = "some message";
          if(mail($_POST['email'],"Please verify your account",$msg))
          {
              $mailFlag = "success";
          }
          else
          {
              $mailFlag = "mail_not_sent";
          }
      };
      $auth->registerWithUniqueUsername($_POST['email'], $_POST['password'], $_POST['username'], $callback);
      $output['result'] = $mailFlag; //this is the array where I want to store the result in ("success" of "mail_not_sent").
  }
      catch ($e) {
  }