binoculars
binoculars

Reputation: 2274

Returning value from callback

After registering a new user, I receive a selector and token for account verification. I want to be able to know whether or not a confirmation mail was sent, but I'm having troubles returning the value from the callback. Here's what I have:

  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) {
  }

Upvotes: 1

Views: 2706

Answers (2)

Nikolai
Nikolai

Reputation: 195

This is a bit odd way but:

       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) {
  }

Upvotes: 0

Sean Bright
Sean Bright

Reputation: 120644

It doesn't look like Auth::registerWithUniqueUsername() gives you access to the result of the callback, so if I had to do this, I would do something like this:

$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;

Upvotes: 2

Related Questions