Get support for guhemama/http-precondition-bundle

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

guhemama/http-precondition-bundle

Packagist Version Packagist Downloads

HTTP Precondition Bundle

This bundle introduces a Precondition attribute that can be used to check for certain conditions when routing. When the conditions are not met, an exception is thrown (412 Precondition failed).

Installation

Install the bundle with Composer:

$ composer require guhemama/http-precondition-bundle

Usage

To define a new precondition, import the Guhemama\HttpPreconditionBundle\Annotations\Precondition attribute and provide an expression expr to be evaluated - any valid ExpressionLanguage expression is accepted.

<?php

namespace App\Controller;

use Guhemama\HttpPreconditionBundle\Annotations\Precondition;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;

class QuestionController extends AbstractController
{
    #[Precondition(expr: "1+1 > 2")]
    #[Route('/question')]
    public function index(): JsonResponse
    {
        return $this->json(['answer' => 42]);
    }
}

When using the ParamConverter (Symfony 5) or the MapEntity (Symfony 6+) attributes, you can also refer to the mapped entities in the precondition expression:

<?php

use App\Entity\Question;
use Guhemama\HttpPreconditionBundle\Annotations\Precondition;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;

class QuestionController extends AbstractController
{
    #[Precondition(expr: '!question.isAnswered()', message: 'Cannot answer an already answered question.', payload: ['error' => 'QUESTION_ALREADY_ANSWERED'])]
    #[Route(path: '/question/{id}', methods: ['POST'])]
    public function update(
        #[MapEntity(Question::class)] Question $question
    ): Response
    {
        return new JsonResponse($question);
    }
}

When the precondition expression evaluates to false, an \Guhemama\HttpPreconditionBundle\Exception\Http\PreconditionFailedHttpException exception is thrown. This exception also includes an instance of the Precondition should you need access to its configured values (e.g. payload).

Configuration

This bundle depends on the ExpressionLanguage component. If you have extended the expression language or would like to use a another instance of it instead of the default one, update the configuration as follows, replacing my_custom_expression_lang_service with your service name:

# config/packages/guhemama_http_precondition.yaml
guhemama_http_precondition:
  expression_language: my_custom_expression_lang_service
Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2