PHPackages                             onursimsek/precondition - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. onursimsek/precondition

ActiveLibrary[HTTP &amp; Networking](/categories/http)

onursimsek/precondition
=======================

HTTP Precondition implementation for Laravel

2.0.3(2w ago)44401MITPHPPHP ^8.2|^8.3|^8.4|^8.5CI passing

Since Mar 6Pushed 2w ago1 watchersCompare

[ Source](https://github.com/onursimsek/precondition)[ Packagist](https://packagist.org/packages/onursimsek/precondition)[ GitHub Sponsors](https://github.com/onursimsek)[ RSS](/packages/onursimsek-precondition/feed)WikiDiscussions master Synced today

READMEChangelog (5)Dependencies (10)Versions (7)Used By (0)

   ![Package Image](https://camo.githubusercontent.com/1e8d3a3680bd315a2c5175792c6cc23b7680eb0d8c6ca6bfa00f3686d07037ac/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f507265636f6e646974696f6e2e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6f6e757273696d73656b253246707265636f6e646974696f6e267061747465726e3d746f706f677261706879267374796c653d7374796c655f32266465736372697074696f6e3d485454502b507265636f6e646974696f6e2b666f722b4c61726176656c266d643d312673686f7757617465726d61726b3d3126666f6e7453697a653d313235707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)HTTP Precondition for Laravel
=============================

[](#http-precondition-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/bcfb39c645ce9e4e6f2d9422d90712a39582832e3d5fa07c6de56d14996d8f00/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6e757273696d73656b2f707265636f6e646974696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/onursimsek/precondition)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Tests](https://github.com/onursimsek/precondition/actions/workflows/run-tests.yml/badge.svg)](https://github.com/onursimsek/precondition/actions)[![Total Downloads](https://camo.githubusercontent.com/b8716c65d6a87290628847cb018da23e62319e71c48d601e30c730b0e2458b7f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f6e757273696d73656b2f707265636f6e646974696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/onursimsek/precondition)

Installation
------------

[](#installation)

To install, run the following command in your project:

```
composer require onursimsek/precondition
```

Usage
-----

[](#usage)

A client has requested a resource from you (GET), and after a while wants to update the resource and send it back to you (PUT). Meanwhile, another client has received the same resource (GET) and updated it before the first client (PUT). In this case, the first client's update will be based on an incorrect copy and will ignore the second client's updates. This is called **[lost update problem](https://www.rfc-editor.org/rfc/rfc6585.txt)**.

In such a case, you can return **[428 Precondition Required](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/428)** to the first client and ask it to pull the resource again.

This package allows you to do this easily. All you need to do is create a **Validator class** and add it as an **attribute** to the corresponding controller method.

```
use Illuminate\Http\Request;
use OnurSimsek\Precondition\Validators\PreconditionValidator;

class LostUpdateValidator extends PreconditionValidator
{
    public function parameter(Request $request)
    {
        return $request->header('If-Unmodified-Since');
    }

    public function __invoke(Request $request): bool
    {
        return $this->parameter($request) == $request->route('article')->updated_at;
    }
}
```

```
use App\Models\Article;
use App\Http\Requests\UpdateArticleRequest;
use OnurSimsek\Precondition\Attributes\Precondition;

class ArticleController
{
    #[Precondition(LostUpdateValidator::class)]
    public function update(Article $article, UpdateArticleRequest $request)
    {
        $article->fill($request->validated())
        $article->save();

        return response()->json();
    }
}
```

Finally, you need to add **precondition** middleware to the **route** you want to use.

```
Route::put('/articles/{:article}', [ArticleController::class, 'update'])
    ->middleware(['precondition']);
```

You can also use this package if you have any conditions for a request to be fulfilled. For example some examples;

### Github Sample

[](#github-sample)

You are prompted to re-type the repository name before deleting a repository.

```
use Illuminate\Http\Request;
use OnurSimsek\Precondition\Validators\PreconditionValidator;

class DeleteRepositoryValidator extends PreconditionValidator
{
    public function parameter(Request $request)
    {
        return $request->input('repository_name');
    }

    public function __invoke(Request $request): bool
    {
        return $this->parameter($request) == $request->route('repository')->name;
    }
}
```

**Note:** If the repo name is invalid, the response returns with a status code of **[412 Precondition Failed](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/412)**.

### SMS Verification

[](#sms-verification)

SMS verification is a mandatory step in the payment process.

```
use Illuminate\Http\Request;
use OnurSimsek\Precondition\Validators\PreconditionValidator;

class SmsValidator extends PreconditionValidator
{
    public function preProcess(): void
    {
        cache()->set('sms', '123456');
    }

    public function parameter(Request $request)
    {
        return $request->input('sms_code');
    }

    public function __invoke(Request $request): bool
    {
        return $this->parameter($request) == cache()->get('sms');
    }
}
```

### Optional Verification

[](#optional-verification)

If you want to validate on a case-by-case basis, you can use the **when** method as follows.

```
use Illuminate\Http\Request;
use OnurSimsek\Precondition\Validators\PreconditionValidator;

class PrivateArticleValidator extends PreconditionValidator
{
    public function when(Request $request)
    {
        return $request->route('article')->is_private;
    }

    public function parameter(Request $request)
    {
        return $request->header('X-Article-Secret-Code');
    }

    public function __invoke(Request $request): bool
    {
        return $this->parameter($request) == $request->route('article')->secret_code;
    }
}
```

You can use the **messages()** method in the validator class to customize the error messages.

```
public function messages(): array
{
    return [
        'required' => 'Enter the sms code sent to your phone.',
        'failed' => 'Incorrect sms code!',
    ];
}
```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Onur Simsek](https://github.com/onursimsek)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

51

↑

FairBetter than 95% of packages

Maintenance96

Actively maintained with recent releases

Popularity22

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 53.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~207 days

Total

5

Last Release

19d ago

Major Versions

1.0.0 → 2.0.02024-05-17

PHP version history (2 changes)1.0.0PHP ^8.1|^8.2|^8.3

2.0.2PHP ^8.2|^8.3|^8.4|^8.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1241396?v=4)[Onur Şimşek](/maintainers/onursimsek)[@onursimsek](https://github.com/onursimsek)

---

Top Contributors

[![onursimsek](https://avatars.githubusercontent.com/u/1241396?v=4)](https://github.com/onursimsek "onursimsek (21 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (12 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (6 commits)")

---

Tags

laravel-packagelost-updatepreconditionprecondition-validatorrequestlaravelprecondition

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/onursimsek-precondition/health.svg)

```
[![Health](https://phpackages.com/badges/onursimsek-precondition/health.svg)](https://phpackages.com/packages/onursimsek-precondition)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k95.4M307](/packages/laravel-horizon)[laravel/sail

Docker files for running a basic Laravel application.

1.9k205.7M1.3k](/packages/laravel-sail)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.6k](/packages/illuminate-database)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M195](/packages/laravel-ai)[moonshine/moonshine

Laravel administration panel

1.3k253.1k81](/packages/moonshine-moonshine)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
