We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon and PSR-7 message interfaces.

Hi, I'm trying to create a new Oauth server using this library: https://github.com/thephpleague/oauth2-server

My problem is that they are using the PSR-7 message interface, so if I want to validate a request I have to pass a ResponseInterface and a ServerRequestInterface to the validation method.

I want to know if is it possible to get such interfaces or if I need to create a new Phalcon request/response object implementing the interfaces or do something different.

Sorry I'm a complete newbie and need help on this, I'm using Phalcon v2.1.0 RC1 with PHP 7.0.5

Thanks in advance!

Just implement new response which will implement both phalcon's ResponseInterface and psr-7 responseinterface. Also you can create something like ResponseBridge which will implement only psr-7 response interface and which will return proper values from phalcon's response accesed using di.



12.2k
edited Jun '16

Hey,

I'm currently in the process of implementing something similar myself, and after creating a convertor method to try to do this and going down a long-winded path, their is a better way. Import Guzzle PSR7 into your project and you can instantiate a request object with a convenience method as shown:

    use GuzzleHttp\Psr7\ServerRequest;
     //in your controller action method
     $response = new PhalconResponsePSR7(); // I'm still working on this part
        try {

            // Try to respond to the access token request
            $serverRequest = ServerRequest::fromGlobals(); // instantiates a Guzzle serverRequest  from the global request vars
            echo $server->respondToAccessTokenRequest($serverRequest, $response);

        } catch (OAuthServerException $exception) {

        // All instances of OAuthServerException can be converted to a PSR-7 response
        $this->view->disable();

        //Create a response instance
        $presponse = new \Phalcon\Http\Response();

        //Set the content of the response
        $presponse->setContent($exception->generateHttpResponse($response)->getBody());

        //Return the response
        return $presponse;

    } catch (\Exception $exception) {

        // Catch unexpected exceptions
        $body = $response->getBody();
        $body->write($exception->getMessage());
        echo $body;

    }

This is very basic and I'm still working on the response (though that's slightly more simple I believe) but it should get you started

@TommyBs I have the same problem, please let me know when You will finish PhalconResponsePSR7

Hey,

I'm currently in the process of implementing something similar myself, and after creating a convertor method to try to do this and going down a long-winded path, their is a better way. Import Guzzle PSR7 into your project and you can instantiate a request object with a convenience method as shown:

  use GuzzleHttp\Psr7\ServerRequest;
   //in your controller action method
   $response = new PhalconResponsePSR7(); // I'm still working on this part
      try {

           // Try to respond to the access token request
           $serverRequest = ServerRequest::fromGlobals(); // instantiates a Guzzle serverRequest  from the global request vars
           echo $server->respondToAccessTokenRequest($serverRequest, $response);

      } catch (OAuthServerException $exception) {

       // All instances of OAuthServerException can be converted to a PSR-7 response
       $this->view->disable();

       //Create a response instance
       $presponse = new \Phalcon\Http\Response();

       //Set the content of the response
       $presponse->setContent($exception->generateHttpResponse($response)->getBody());

       //Return the response
       return $presponse;

   } catch (\Exception $exception) {

       // Catch unexpected exceptions
       $body = $response->getBody();
       $body->write($exception->getMessage());
       echo $body;

   }

This is very basic and I'm still working on the response (though that's slightly more simple I believe) but it should get you started

I've created such class:


class ResponseParser extends Plugin
{
    public static function parse(ResponseInterface $psr7Response, \Phalcon\Http\Response $phalconResponse)
    {
        $stream = $psr7Response->getBody();
        $stream->rewind();

        return $phalconResponse
            ->setStatusCode($psr7Response->getStatusCode())
            ->setContent($stream->getContents());
    }
}


293

I have the same problem, I use a method to convert psr7response to phalcon response


    private function convertResponse(\Psr\Http\Message\ResponseInterface $psr7Response, ResponseInterface $response): ResponseInterface
    {
        $response->setStatusCode($psr7Response->getStatusCode(), $psr7Response->getReasonPhrase());
        foreach ($psr7Response->getHeaders() as $name => $values) {
            foreach ($values as $value) {
                $response->setHeader($name, $value);
            }
        }
        $body = $psr7Response->getBody();
        $body->seek(0);
        $response->setContent($body->getContents());

        return $response;
    }

https://github.com/runphp/eelly-oauth2-server/blob/master/src/Middleware/Traits/ResponseTrait.php