Flarum\Api\Client::send PHP Method

send() public method

Execute the given API action class, pass the input and return its response.
public send ( string | Flarum\Http\Controller\ControllerInterface $controller, User | null $actor, array $queryParams = [], array $body = [] ) : Psr\Http\Message\ResponseInterface
$controller string | Flarum\Http\Controller\ControllerInterface
$actor Flarum\Core\User | null
$queryParams array
$body array
return Psr\Http\Message\ResponseInterface
    public function send($controller, $actor, array $queryParams = [], array $body = [])
    {
        $request = ServerRequestFactory::fromGlobals(null, $queryParams, $body);
        $request = $request->withAttribute('actor', $actor);
        if (is_string($controller)) {
            $controller = $this->container->make($controller);
        }
        if (!$controller instanceof ControllerInterface) {
            throw new InvalidArgumentException('Endpoint must be an instance of ' . ControllerInterface::class);
        }
        try {
            return $controller->handle($request);
        } catch (Exception $e) {
            return $this->errorHandler->handle($e);
        }
    }

Usage Example

 /**
  * @param Request $request
  * @return JsonResponse|EmptyResponse
  */
 public function handle(Request $request)
 {
     $actor = $request->getAttribute('actor');
     $Referer = $request->getHeader('Referer');
     $params = array_only($request->getParsedBody(), ['identification', 'password']);
     $response = $this->apiClient->send(TokenController::class, $actor, [], $params);
     if ($response->getStatusCode() === 200) {
         $data = json_decode($response->getBody());
         $session = $request->getAttribute('session');
         $this->authenticator->logIn($session, $data->userId);
         $token = AccessToken::find($data->token);
         event(new UserLoggedIn($this->users->findOrFail($data->userId), $token));
         $response = FigResponseCookies::set($response, SetCookie::create("lastLoginName")->withValue($request->getParsedBody()['identification'])->withPath('/'));
         $response = $this->rememberer->remember($response, $token);
     } elseif ($response->getStatusCode() === 401) {
         $responseNew = $this->apiClient->send(PingxxTokenController::class, $actor, [], $params);
         if ($responseNew->getStatusCode() === 200) {
             $data = json_decode($responseNew->getBody());
             $session = $request->getAttribute('session');
             $this->authenticator->logIn($session, $data->userId);
             $token = AccessToken::find($data->token);
             event(new UserLoggedIn($this->users->findOrFail($data->userId), $token));
             $responseNew = FigResponseCookies::set($responseNew, SetCookie::create("lastLoginName")->withValue($request->getParsedBody()['identification'])->withPath('/')->withDomain('dashboard.pingxx.com'));
             $responseNew = $this->rememberer->remember($responseNew, $token);
             return $responseNew;
         } else {
             return $response;
         }
     }
     return $response;
 }
All Usage Examples Of Flarum\Api\Client::send