PHP code example of mogody / hyperf-responsable
1. Go to this page and download the library: Download mogody/hyperf-responsable library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mogody / hyperf-responsable example snippets
namespace App\Response;
use Mogody\Responsable\Contract\Responsable;
use Psr\Http\Message\RequestInterface as PsrRequestInterface;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
class ExampleObject implements Responsable
{
public function __construct($name = null)
{
$this->name = $name ?? 'Teapot';
}
public function status()
{
switch(strtolower($this->name)) {
case 'teapot':
return 418;
default:
return 200;
}
}
public function toResponse(PsrRequestInterface $request): PsrResponseInterface
{
$response = \Hyperf\Utils\Context::get(PsrResponseInterface::class);
return $response->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream("Hello {$this->name}"));
}
}
namespace App\Controller;
use App\Response\ExampleObject;
class PostsController
{
public function index()
{
return new ExampleObject('Taylor');
}
}