PHP code example of solidworx / simple-response-bundle
1. Go to this page and download the library: Download solidworx/simple-response-bundle 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/ */
solidworx / simple-response-bundle example snippets
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = [
...
new SolidWorx\SimpleResponseBundle\SimpleResponseBundle(),
...
];
...
}
}
// src/AppBundle/Action/MyAction.php
use SolidWorx\SimpleResponseBundle\Response\TemplateResponse;
class MyAction
{
public function __invoke()
{
return new TemplateResponse('index.html.twig');
}
}
// src/AppBundle/Action/MyAction.php
use SolidWorx\SimpleResponseBundle\Response\RouteRedirectResponse;
class MyAction
{
public function __invoke()
{
return new RouteRedirectResponse('_some_route_name');
}
}
use Symfony\Component\HttpFoundation\JsonResponse;
class DoctrineEntityResponse extends JsonResponse
{
private $entity;
public function __construct(string $entity)
{
$this->entity = $entity;
parent::__construct();
}
public function getEntity(): string
{
return $this->entity;
}
}
use SolidWorx\SimpleResponseBundle\ResponseHandlerInterface;
use Symfony\Component\HttpFoundation\Response;
class Handler implements ResponseHandlerInterface
{
private $em;
public function __construct($entityManager)
{
$this->em = $entityManager;
}
public function supports(Response $object): bool
{
return $object instanceof DoctrineEntityReponse; // Only support responses of this type
}
public function handle(Response $object): Response
{
return $object->setData($this->em->getRepository($object->getEntity())->findAll()); // Return all records in the entity as a JSON response
}
}
// src/AppBundle/Action/MyAction.php
class MyAction
{
public function __invoke()
{
return new DoctrineEntityResponse(\App\Entity\Order::class); // Pass the Order entity which will return all orders in a JSON response
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.