PHP code example of visual-craft / rest-base-bundle
1. Go to this page and download the library: Download visual-craft/rest-base-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/ */
visual-craft / rest-base-bundle example snippets
// AppKernel.php
// ...
use VisualCraft\RestBaseBundle\VisualCraftRestBaseBundle;
class AppKernel extends Kernel
{
public function registerBundles()
{
return [
// ...
new VisualCraftRestBaseBundle(),
// ...
];
}
}
declare(strict_types=1);
namespace App\Exceptions;
class CustomException extends \RuntimeException
{
private string $customField;
public function __construct(string $customField, $message = "")
{
parent::__construct($message);
$this->customField = $customField;
}
public function getCustomField(): string
{
return $this->customField;
}
}
//src/Problem/ExceptionToProblemConverters/InvalidRequestBodyFormatExceptionConverter.php
declare(strict_types=1);
namespace VisualCraft\RestBaseBundle\Problem\ExceptionToProblemConverters;
use Symfony\Component\HttpFoundation\Response;
use VisualCraft\RestBaseBundle\Problem\ExceptionToProblemConverterInterface;
use VisualCraft\RestBaseBundle\Problem\Problem;
class CustomExceptionConverter implements ExceptionToProblemConverterInterface
{
public function convert(\Throwable $exception): ?Problem
{
if (!$exception instanceof CustomException) {
return null;
}
$result = new Problem(
'Custom exception title',
Response::HTTP_BAD_REQUEST,
'custom_exception_type'
);
$result->addDetails('customField', $exception->getCustomField());
return $result;
}
}
//src/Controller
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
class ThrowInvalidRequestExceptionController extends AbstractController
{
public function __invoke(Request $request): void
{
//..
throw new CustomException('custom field value');
//..
}
}
// src/Controller/ProcessRequestController.php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use VisualCraft\RestBaseBundle\Request\RequestBodyDeserializer;
class ProcessRequestController extends AbstractController
{
private RequestBodyDeserializer $deserializer;
public function __construct(RequestBodyDeserializer $deserializer)
{
$this->deserializer = $deserializer;
}
public function __invoke(Request $request): Response
{
//..
$testDto = $this->deserializer->deserialize($request, Dto::class);
//..
}
}
// src/Controller/ProcessRequestController.php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use VisualCraft\RestBaseBundle\Request\RequestBodyDeserializer;
class ProcessRequestController extends AbstractController
{
private FailingValidator $validator;
public function __construct(
FailingValidator $validator
) {
$this->validator = $validator;
}
public function __invoke(Request $request): Response
{
//..
$this->validator->validate($data);
//..
}
}
php
declare(strict_types=1);
namespace App\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
use VisualCraft\RestBaseBundle\Constants;
class AuthenticationEntryPoint implements AuthenticationEntryPointInterface
{
private ProblemResponseFactory $problemResponseFactory;
public function __construct(ProblemResponseFactory $problemResponseFactory)
{
$this->problemResponseFactory = $problemResponseFactory;
}
public function start(Request $request, AuthenticationException $authException = null): Response
{
if ($request->attributes->get(Constants::API_ZONE_ATTRIBUTE)) {
return $this->problemResponseFactory->create($authException ?? new AuthenticationException('Authentication
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.