What’s new in 3.4
Version 3.4 lets your application keep working after the response is sent, adds the HTTP QUERY method, and makes
queues and mail delivery safer.
To upgrade, follow Upgrading from version 3.3 to 3.4.
Work after the response
Some work does not need to delay the response: sending an email, calling a webhook, warming a cache. You can now run it after the response has been sent to the client, in the same PHP process. Terminate the application in your front controller:
$app = new HttpApp();
$response = $app->handle();
$app->print($response);
$app->terminate();
Then listen to the terminate event, or use a deferred queue. With PHP-FPM or LiteSpeed, the client does not wait for this work.
See After the response.
Deferred queues
A deferred queue is consumed in the same process, once the response has been sent or at the end of a CLI command. No worker is needed. Declare a memory queue and list it as deferred:
{
"berlioz": {
"queues": {
"queues": [
{
"type": "Berlioz\\QueueManager\\Queue\\MemoryQueue",
"name": "deferred",
"max_attempts": 1
}
],
"deferred": {
"queues": ["deferred"]
}
}
}
}
Push jobs to it as to any other queue. The same job handlers process them, so you can move a job to a persistent queue later without changing your code.
See Deferred queues.
HTTP QUERY method
Routes accept the QUERY method of RFC 10008: a safe and idempotent
request that carries its query in the body. The acceptQuery route option declares the accepted formats and rejects
the other ones:
#[Berlioz\Route('/contacts', method: ['GET', 'QUERY'], acceptQuery: ['application/json'])]
public function search(ServerRequestInterface $request): array
{
$criteria = $request->getParsedBody() ?? $request->getQueryParams();
// ...
}
The HTTP client gets a Client::query() method, and forms read the body of QUERY requests.
See QUERY method.
Encrypted SMTP connections
The SMTP transport supports STARTTLS and implicit TLS. Encryption is mandatory once requested: the transport never falls back to a plain connection.
$smtp = new Smtp(
host: 'smtp.test.com',
username: 'user@test.com',
password: 'password',
port: 587,
encryption: SmtpEncryption::STARTTLS,
timeout: 5,
);
Queues
More queue types in the configuration
Redis, AMQP, RabbitMQ and Null queues can be declared in the configuration, like database, SQS and memory queues:
{
"type": "Berlioz\\QueueManager\\Queue\\RedisQueue",
"redis": {
"host": "127.0.0.1",
"port": 6379
},
"name": ["emails", "notifications"]
}
See Redis queue, AMQP queue and RabbitMQ queue.
Last attempt of a job
A job handler can know that the current attempt is the last one, and act differently when the job will not be retried:
public function handle(JobInterface $job): void
{
try {
$this->mailer->sendOrderConfirmation($order, $email);
} catch (MailerException $exception) {
if ($job->isLastAttempt()) {
$this->orders->flagConfirmationFailure($order);
return;
}
throw $exception;
}
}
See Handling the last attempt.
Safer retries
- Redis queues stop retrying a job after
maxAttemptsand no longer lose a job when a worker dies while processing it. - AMQP queues count the messages redelivered by the broker as attempts.
- Every queue exposes its maximum attempts and its retry time.
See Attempts and retry time and Delivery guarantees and idempotency.
Other new features
- Forms:
ApiDataProviderkeeps JSON types withstrictJson: trueand handlesPATCHandDELETErequests. See Data providers. - Forms:
handle($request, apply: false)validates a submission without modifying the mapped object, andapply()writes it once it is valid. See Validate before writing. - Forms: inputs reject arrays and objects submitted as their value. See Non-scalar values.
- Forms: constraints added with
invalid()make the element and its parents invalid, until the form is handled again orresetConstraints()is called. See Manual invalidation. - HTTP: a malformed body of the incoming request, such as invalid JSON, produces a 400 Bad Request instead of a 500. See Malformed bodies.
- HTTP:
HttpApp::print()validates the complete response before sending it: an invalid status line or header is never sent halfway. See HTTP/1 emission. - HTTP client: the TLS client certificate and the credentials given to the cURL adapter are no longer sent when
a redirect leaves the initial origin;
credentials_originslists the other origins allowed to receive them. See Transport credentials. - Controllers:
jsonResponse()throws a 500 error when the encoding fails, instead of returning an empty 204 response. See Response helpers. - HTTP client: the cURL and stream adapters use the proxy of the environment variables in the same way, and
'proxy' => falsedisables it. See Proxy. - Hector ORM: PDO options accept constant names such as
PDO::ATTR_TIMEOUT. See PDO options. - Debug: debug access is restricted to
127.0.0.1and::1by default; allow other clients with IP addresses, CIDR ranges or aliases such asprivate. See Restrict access. - Debug: the toolbar shows the response time, and the console shows a waiting page while a request still works after its response. See Debug.
- Twig:
entrypoints()preloads every file it renders. See Preload. - Queue worker:
Worker::drain()processes a queue until it is empty. See Drain a queue. - Queue manager:
QueueManager::getQueue()returns a queue by its name, for example to read its settings. See Get a queue.
Deprecations
Declaring services in the container.services configuration key, the scalar option of form inputs and four other
features are deprecated. See Preparing for version 4.