PHPackages                             laragear/preload - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. laragear/preload

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

laragear/preload
================

Effortlessly make a Preload script for your Laravel application.

v4.0.0(2mo ago)119363.5k↑12.3%8MITPHPPHP ^8.3CI passing

Since Feb 16Pushed 2mo ago4 watchersCompare

[ Source](https://github.com/Laragear/Preload)[ Packagist](https://packagist.org/packages/laragear/preload)[ Fund](https://github.com/sponsors/DarkGhostHunter)[ Fund](https://paypal.me/darkghosthunter)[ RSS](/packages/laragear-preload/feed)WikiDiscussions 4.x Synced 1mo ago

READMEChangelog (9)Dependencies (20)Versions (16)Used By (0)

Preload
=======

[](#preload)

[![Latest Version on Packagist](https://camo.githubusercontent.com/4b576559d88b7dd2bf74303fd06da7d659382e6a618a80e324ef6696ffb62b43/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c617261676561722f7072656c6f61642e737667)](https://packagist.org/packages/laragear/preload)[![Latest stable test run](https://github.com/Laragear/Preload/workflows/Tests/badge.svg)](https://github.com/Laragear/Preload/actions)[![Codecov coverage](https://camo.githubusercontent.com/e996dd8ae2118335b356b8e365e12e6ed36848e991a8640334cc259c2459eaf2/68747470733a2f2f636f6465636f762e696f2f67682f4c617261676561722f5072656c6f61642f67726170682f62616467652e7376673f746f6b656e3d4450474f3142444a434a)](https://codecov.io/gh/Laragear/Preload)[![Maintainability](https://camo.githubusercontent.com/7c94e09cfe0779e7d257aa3eb99b3ac9ddae6590bcb7d7aebc3290b089f47ac0/68747470733a2f2f716c74792e73682f6261646765732f39303039613437322d353935312d346334382d383735662d3534633961393931363932612f6d61696e7461696e6162696c6974792e737667)](https://qlty.sh/gh/Laragear/projects/Preload)[![Sonarcloud Status](https://camo.githubusercontent.com/78d3ce7a2f880e1a4425407a3f2ffa12417c619a88ebd6d231de0f8f7571abb0/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d4c617261676561725f5072656c6f6164266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=Laragear_Preload)[![Laravel Octane Compatibility](https://camo.githubusercontent.com/70359a356da237cd29561bc5d0bb80baae775b5ff62f288ed324755382858342/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2532304f6374616e652d436f6d70617469626c652d737563636573733f7374796c653d666c6174266c6f676f3d6c61726176656c)](https://laravel.com/docs/9.x/octane#introduction)

Dynamically preload your Laravel application.

This package generates a [PHP preloading](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.preload) script from your Opcache statistics automatically. No need to hack your way in.

Become a sponsor
----------------

[](#become-a-sponsor)

[![](.github/assets/support.png)](https://github.com/sponsors/DarkGhostHunter)

Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **spread the word on social media!**

Requirements
------------

[](#requirements)

- PHP 8.3 or later
- Laravel 12 or later
- [Opcache &amp; Preloading enabled](https://www.php.net/manual/en/book.opcache.php) (`ext-opcache`).

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

[](#installation)

Require this using Composer into your project

```
composer require laragear/preload
```

Note

This package doesn't require the `ext-opcache` extension to install. Just be sure to have it [enabled in your deployment server](https://www.php.net/manual/en/book.opcache.php).

What is Preloading? Does it make my app FAST?
---------------------------------------------

[](#what-is-preloading-does-it-make-my-app-fast)

PHP interpreter needs to read and compile each requested file in your project. When Opcache is enabled, it will keep interpreted files in memory instead of reading them again from the file system, which is miles faster.

Opcache's Preloading allows storing in memory a given list of files when the PHP process starts, before normal execution. This makes the application *faster* for first requests, as these files to read are already in memory. With [JIT](https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.jit), these files are also compiled into byte-code, saving another step.

This package generates a file with a list containing the most accessed files of your application and a script that will load these files on startup. You can point the "loader" script into your `php.ini`:

```
opcache.preload_user = 'www-data'
opcache.preload = '/www/app/preload.php';
```

After that, the next time PHP starts, this list of files will be preloaded automatically.

Note

If you're behind a shared server, preloading may be not available for your application. Normally, shared servers also share the same PHP process, and its configuration file (`php.ini`) is not available for modification. Check your server if you're not sure if Laragear Preload should be installed.

Usage
-----

[](#usage)

By default, this package pushes a queued job each 10,000 requests, containing a limited list with the most accessed files of the application. [This condition can be changed](#custom-condition).

First, you should publish the stub script with the `preload:stub` Artisan command. By default, it will copy a stub preloader into the application root directory [by default](#paths).

```
php artisan preload:stub

# Stub copied at [/www/app/preload.php]
#
# Remember to edit your [php.ini] file:
# opcache.preload = /www/app/preload.php;
```

This way, you can add the preload file path in your `php.ini` as instructed by the command.

```
opcache.preload = '/www/app/preload.php';
```

That's it. At the 10,000th request, the preloader stub will be replaced by a real preload script along with the list of files that should be warmed up by PHP at startup.

Custom condition
----------------

[](#custom-condition)

This package includes a simple condition callback: each 10,000 requests, generate a Preloading script.

If this condition is not enough for your application, or you require a custom condition, you can easily create a callback or even an *invokable* class with your own logic. The callable will be resolved by the application container, and run after the request has been sent to the browser.

Once you create the condition, register it through the `condition()` method of the `Preloader` facade. You can do this in your `App\Providers\AppServiceProvider` or `bootstrap/app.php`.

```
use Illuminate\Http\Request;
use Illuminate\Foundation\Application;
use Illuminate\Support\Lottery;
use Laragear\Preload\Facades\Preload;

return Application::configure()
    ->registered(function () {
        Preload::condition(function (Request $request) {
            if ($request->user()?->isAdmin()) {
                return false;
            }

            return random_int(0, 100) === 50;
        });
    })->create();
```

You may also return a `Illuminate\Support\Lottery` instance for convenience, which is great for testing purposes.

```
use Illuminate\Support\Lottery;
use Laragear\Preload\Facades\Preload;

Preload::condition(function (Request $request) {
    if ($request->user()?->isAdmin()) {
        return false;
    }

    return Lottery::odds(2, 100);
});
```

Include and exclude
-------------------

[](#include-and-exclude)

To include or exclude PHP files or entire directory paths from the Preload list, use the `include()` and `exclude()` methods from the `Preload` facade, respectively.

Both methods accept an array of [glob patterns](https://en.wikipedia.org/wiki/Glob_(programming)) or a callback that receives the [Symfony Finder](https://symfony.com/doc/current/components/finder.html) for greater filtering options. On both cases, only `.php` files will be included in the list.

```
use Laragear\Preload\Facades\Preload;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\Finder\Finder;

return Application::configure()
    ->booted(function () {
        Preload::include(base_path('/services/**'));

        Preload::exclude(function (Finder $find) {
            $find->in(base_path('/temp/'))->contains('class ');
        });
    })
    ->create();
```

Important

Included files will be *appended* to the list. This means that exclusion logic will run first.

### Excluding and including libraries

[](#excluding-and-including-libraries)

You can easily exclude or include entire libraries from your application by setting them at the `extra.preload` key of your `composer.json`, respectively.

The `preload` object should contain the library name as key, and `false` to exclude it or `true` to include. If you require fine-tuning, you can use an object with `exclude` and `include` files and a glob pattern for the file or files you wish to exclude or include, respectively. These patterns will be fed to the underlying Symfony Finder.

```
{
    "extra": {
        "preload": {
            "laragear/meta": {
                "exclude": ["src/Foo/*", "src/Bar/*/**"],
                "include": "src/Request/*"
            },
            "charlesdp/builder": false
        }
    }
}
```

Important

Included libraries will be *appended* to the list. This means that exclusion logic will run first.

Configuration
-------------

[](#configuration)

Some people may not be happy with the "default" behaviour. Luckily, you can configure your own way to generate the script.

First publish the configuration file:

```
php artisan vendor:publish --provider="Laragear\Preload\PreloadServiceProvider"
```

Let's check the config array:

```
