Skip to content

Instantly share code, notes, and snippets.

View ismail1432's full-sized avatar
🏠
Looking for remote job

Smaine Milianni ismail1432

🏠
Looking for remote job
View GitHub Profile
// Request
PUT /posts/1
{
"title": "such a title",
"content": "Api Platform is awesome"
}
// Response
{
"@context": "/contexts/ConstraintViolationList",
"@type": "ConstraintViolationList",
"hydra:title": "An error occurred",
"hydra:description": "title: You are not allowed to do this stuff !",
"violations": [
{
"propertyPath": "title",
"message": "You are not allowed to do this stuff !"
{
"@context": "/contexts/Post",
"@id": "/posts/1",
"@type": "Post",
"id": 1,
"title": "such a title",
"content": "Api Platform is awesome"
}
<?php
class FooRequestListener
{
public function onKernelRequest(RequestEvent $event)
{
if ($this->supports($event) {
// condition fullfilled do some stuff
}
}
<?php
class UserSubscriber
{
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
// if this listener only applies to certain entity types,
// add some code to check the entity type as early as possible
if (!$this->supports($entity)) {
<?php
class FooRequestListener
{
private $enabled = true;
// ..
public function supports(RequestEvent $event): bool
{
return $event->isMasterRequest() && $this->enabled;
}
}
<?php
class UserSubscriber
{
private $enabled = true;
// ...
public function supports($entity): bool
{
return $entity instanceof User && $this->enabled;
}
}
final class DisableListenerEvent
{
public const NAME = 'app.disable_listener';
}
trait DisableListenerTrait
{
private $enabled = true;
public function disable(): void
{
$this->enabled = false;
}
}
<?php
interface DisableListenerInterface
{
public function disable(): void;
}