PHPackages                             leuverink/asset-injector - 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. leuverink/asset-injector

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

leuverink/asset-injector
========================

Automatically inject your package assets onto the page

3.0.2(3mo ago)64.8k2MITPHPPHP ^8.2CI passing

Since Aug 16Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/gwleuverink/asset-injector)[ Packagist](https://packagist.org/packages/leuverink/asset-injector)[ RSS](/packages/leuverink-asset-injector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (10)Versions (8)Used By (2)

Auto Inject Package Assets
==========================

[](#auto-inject-package-assets)

[![codestyle](https://github.com/gwleuverink/inject-package-assets/actions/workflows/codestyle.yml/badge.svg)](https://github.com/gwleuverink/inject-package-assets/actions/workflows/codestyle.yml)[![tests](https://github.com/gwleuverink/inject-package-assets/actions/workflows/tests.yml/badge.svg)](https://github.com/gwleuverink/inject-package-assets/actions/workflows/tests.yml)

Simplify your Laravel package development with automatic asset injection.

This package allows you to seamlessly insert JavaScript and CSS into web responses without requiring manual inclusion by your package users 🚀

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

[](#installation)

Install the package via Composer:

```
composer require leuverink/asset-injector
```

Usage
-----

[](#usage)

1. After installing, you'll need to create a `AssetInjector` implementation.

```
namespace YourPackage;

use Leuverink\AssetInjector\Contracts\AssetInjector;

class InjectAssets implements AssetInjector
{
    // Used to identify your assets in the HTML response
    public function identifier(): string
    {
        return 'MY_PACKAGE';
    }

    // You can opt in to asset injection by implementing your own checks.
    // For example if a package user can control this via config file, or when your end user hits a middleware
    public function enabled(): bool
    {
        return true;
    }

    // Will inject return value in head tag or before html close if no head is present
    public function inject(): string
    {
        $js = file_get_contents(__DIR__ . '/../build/my-package.js');
        $css = file_get_contents(__DIR__ . '/../build/my-package.css');

        return
