PHPackages                             melchiorkokernoot/laravel-autowire-config - 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. melchiorkokernoot/laravel-autowire-config

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

melchiorkokernoot/laravel-autowire-config
=========================================

Allows configuration injection through auto-wired constructor arguments

V4.0.5(2y ago)1311.7k1[4 PRs](https://github.com/MelchiorKokernoot/laravel-autowire-config/pulls)MITPHPPHP ^8.1CI passing

Since Feb 20Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/MelchiorKokernoot/laravel-autowire-config)[ Packagist](https://packagist.org/packages/melchiorkokernoot/laravel-autowire-config)[ Docs](https://github.com/melchiorkokernoot/laravel-autowire-config)[ RSS](/packages/melchiorkokernoot-laravel-autowire-config/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (12)Versions (29)Used By (0)

[![Banner](https://camo.githubusercontent.com/b298bcd1e6c3b16c14a8596b8ed8b88178704207d08009bb5b67b46e66fa6c35/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f6c61726176656c2d6175746f776972652d636f6e666967732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6d656c6368696f726b6f6b65726e6f6f742532466c61726176656c2d6175746f776972652d636f6e666967267061747465726e3d63697263756974426f617264267374796c653d7374796c655f31266465736372697074696f6e3d416c6c6f77732b636f6e66696775726174696f6e2b696e6a656374696f6e2b7468726f7567682b6175746f2d77697265642b636f6e7374727563746f722b617267756d656e7473266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/b298bcd1e6c3b16c14a8596b8ed8b88178704207d08009bb5b67b46e66fa6c35/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f6c61726176656c2d6175746f776972652d636f6e666967732e706e673f7468656d653d6c69676874267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6d656c6368696f726b6f6b65726e6f6f742532466c61726176656c2d6175746f776972652d636f6e666967267061747465726e3d63697263756974426f617264267374796c653d7374796c655f31266465736372697074696f6e3d416c6c6f77732b636f6e66696775726174696f6e2b696e6a656374696f6e2b7468726f7567682b6175746f2d77697265642b636f6e7374727563746f722b617267756d656e7473266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d313030707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

Configuration autowiring and injection
======================================

[](#configuration-autowiring-and-injection)

[![Latest Version on Packagist](https://camo.githubusercontent.com/41005339d0e5038df45032dec3923560171467c1d261dfef2a473bae16f98a85/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d656c6368696f726b6f6b65726e6f6f742f6c61726176656c2d6175746f776972652d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/melchiorkokernoot/laravel-autowire-config)[![GitHub Tests Action Status](https://camo.githubusercontent.com/079634cda760bb91b0a548e5269e72ed9b7267c8b68f60fc14aa5091f2fc3a00/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6d656c6368696f726b6f6b65726e6f6f742f6c61726176656c2d6175746f776972652d636f6e6669672f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/melchiorkokernoot/laravel-autowire-config/actions?query=workflow%3Arun-tests+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/757606a9a3eb1b59526217d16943c91ffcc8bdc8e60b1275e0de9768f2df0d47/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d656c6368696f726b6f6b65726e6f6f742f6c61726176656c2d6175746f776972652d636f6e6669672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/melchiorkokernoot/laravel-autowire-config)

Enable laravel configuration injection through auto-wired constructor arguments.

```
class Foo {
    public function __construct(
        #[Config('app.name')]
        public string $myConfiguredAppName,
    ){}
}
```

- [Installation](#installation)
- [Configuration](#configuration)
- [Usage](#usage)
    - [Usage through Custom Application class (Recommended)](#usage-through-custom-application-class-recommended)
    - [Usage through Service provider](#usage-through-service-provider)
        - [Usage through attribute autowiring (AttributeStrategy)](#usage-through-attribute-autowiring-attributestrategy)
        - [Usage through constructor property name autowiring (PropNameStrategy)](usage-through-constructor-property-name-autowiring-propnamestrategy)
        - [Accessing the config values](#accessing-the-config-values)
        - [Typed config classes](#typed-config-classes)
        - [Pitfalls](#pitfalls)
- [Testing](#testing)
- [Roadmap](#roadmap)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [Security Vulnerabilities](#security-vulnerabilities)
- [Credits](#credits)
- [License](#license)

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

[](#installation)

You can install the package via composer:

```
composer require melchiorkokernoot/laravel-autowire-config
```

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

[](#configuration)

The package will automatically register itself. You can publish the config file with:

```
php artisan vendor:publish --provider="MelchiorKokernoot\LaravelAutowireConfig\LaravelAutowireConfigServiceProvider"
```

> Note that it is not necessary to publish the config file, nor is it necessary to configure the package. Only change the config file when you know why you are doing it.

This is the contents of the published config file:

```
return [
    //Either AttributeStrategy::class  or AutowiredPropNameStrategy::class
    'strategy' => PropNameStrategy::class,
];
```

You can choose between two strategies:

- `AttributeStrategy` : Uses attributes to inject the config value.
- `PropNameStrategy` (default): Uses constructor promoted property names to inject the config values.

More on the strategies below.

Usage
-----

[](#usage)

### Usage through Custom Application class (Recommended)

[](#usage-through-custom-application-class-recommended)

Starting from version 3.0.0, the package can be used through a custom `Application` class. This is the recommended way of using the package, as it will offer a more "natural" way of using the package.

The only thing one needs to do to enable this behaviour, is to swap out the default `Application` class in bootstrap/app.php with the `MelchiorKokernoot\LaravelAutowireConfig\Application` class.

So change this:

```
$app = new Illuminate\Foundation\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
);
```

Into this:

```
use MelchiorKokernoot\LaravelAutowireConfig\Application;

$app = new Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__),
);
```

Advantages of using the custom application class:

- Allows for primitive datatypes (int, float, bool, string) to be injected directly into the constructor, without a default value. No more need for wrapping the config value in a typed config class!
- Allows using readonly properties in the constructor

Currently this only works with the attribute strategy, but you do not need to configure this. Once you start using the new `Application` class, the package will automatically disable old way of using the package, so no resources are wasted on duplicate resolution.

To demonstrate this, let's take a look at the following example:

```
class Foo {
    public function __construct(
        #[Config('app.name','default value')]
        public string $myConfiguredAppName,
    ){}
}
```

When resolving this `Foo` class from the container, the package will automatically resolve the config value for `app.name` and inject it into the `$myConfiguredAppName` property.

### Usage through Service provider

[](#usage-through-service-provider)

Starting from Version 2.0.0 the package can be used in two ways:

#### Usage through attribute autowiring (AttributeStrategy)

[](#usage-through-attribute-autowiring-attributestrategy)

Firstly, implement the `AutowiresConfigs` interface on your class. Typehint one of the [typed config classes](#typed-config-classes) in your constructor, and use that typehint as an attribute on the property, finally pass the config key as the attribute value.

```
class Foo implements AutowiresConfigs{
    public function __construct(
        #[StringConfig('app.name')]
        public StringConfig $appName,
    ){}
}
```

> Note you do not have to name the variable to match the config key now, but you still have to type-hint the config class.

When using this class from the container (through dependency injection e.g.), the config value will be injected as if you do this:

```
$foo = new Foo(config('app.name'));
```

#### Usage through constructor property name autowiring (PropNameStrategy)

[](#usage-through-constructor-property-name-autowiring-propnamestrategy)

Firstly, implement the `AutowiresConfigs` interface on your class. Typehint one of the [typed config classes](#typed-config-classes) in your constructor, and use the camelCase version of the config key as the property name.

```
class Foo implements AutowiresConfigs{
    public function __construct(
        public StringConfig $appName,
    ){}
}
```

> You need to match the config key to the property name, so `app.name` will become `appName`.

When using this class from the container (through dependency injection e.g.), the config value will be injected as if you do this:

```
$foo = new Foo(config('app.name'));
```

The benefit of this, is that you keep a clear separation between your application logic and your configuration layer. No more service locators, no more `config()` calls in your code, just clean dependencies.

#### Accessing the config values

[](#accessing-the-config-values)

```
This way of access is only required when NOT using the custom Application strategy

```

Because the config values are wrapped in a typed config class, you cannot access the value directly. Instead, you can access the value through the `value` method. For convenience, the `__toString` magic method is also implemented, so you can use the config value as a string (in the case of a value that can be casted to a string,of course) directly. Furthermore, the shorthands `$object->config->v()`and `$object->config->v()` are also available for accessing the value.

```
class Foo implements AutowiresConfigs{
    public function __construct(
        public StringConfig $appName,
    ){}

    public function bar(){
        //Casting to string
        (string) $this->appName

        //Shorthand method call
        $this->appName->value->v();

        //Shorthand property access
        $this->appName->value->v;

        //Ordinary method call
        return $this->appName->value();
    }
}
```

#### Typed config classes

[](#typed-config-classes)

The following config classes are available:

- `ArrayConfig`
- `BooleanConfig`
- `IntegerConfig`
- `NullableArrayConfig`
- `NullableIntegerConfig`
- `NullableStringConfig`
- `StringConfig`

#### Pitfalls

[](#pitfalls)

This package hooks into the afterResolving callback, which means that it will only work for classes that are resolved through the container. This that the config values will only be populated after the constructor has been called, so the values will not be available in the constructor.

Testing
-------

[](#testing)

```
composer test
```

Roadmap
-------

[](#roadmap)

- Add support for primitive types when using the custom `Application` class
- Add support for readonly properties when using the custom `Application` class
- Add support for configuring the way values should be unwrapped (disable shorthand `v()` for example)

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)

- [MelchiorKokernoot](https://github.com/MelchiorKokernoot)
- [All Contributors](../../contributors)

License
-------

[](#license)

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

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance52

Moderate activity, may be stable

Popularity27

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~13 days

Recently: every ~61 days

Total

21

Last Release

909d ago

Major Versions

v0.0.2 → V1.0.02023-02-21

V1.0.0 → V2.0.02023-02-21

V2.0.9 → v3.0.02023-03-13

v3.0.2 → V4.0.02023-03-14

### Community

Maintainers

![](https://www.gravatar.com/avatar/c6acec44c8f326764aeaa4f283ee2e083223568504a9e5ef4d5fb78d4a922225?d=identicon)[MelchiorKokernoot](/maintainers/MelchiorKokernoot)

---

Top Contributors

[![MelchiorKokernoot](https://avatars.githubusercontent.com/u/23347384?v=4)](https://github.com/MelchiorKokernoot "MelchiorKokernoot (57 commits)")

---

Tags

configurationdependency-injectiondeveloper-experiencelaravellaravelMelchiorKokernootlaravel-autowire-config

###  Code Quality

TestsPest

### Embed Badge

![Health badge](/badges/melchiorkokernoot-laravel-autowire-config/health.svg)

```
[![Health](https://phpackages.com/badges/melchiorkokernoot-laravel-autowire-config/health.svg)](https://phpackages.com/packages/melchiorkokernoot-laravel-autowire-config)
```

###  Alternatives

[akaunting/laravel-money

Currency formatting and conversion package for Laravel

7825.3M18](/packages/akaunting-laravel-money)[spatie/laravel-livewire-wizard

Build wizards using Livewire

4061.0M4](/packages/spatie-laravel-livewire-wizard)[tonysm/importmap-laravel

Use ESM with importmap to manage modern JavaScript in Laravel without transpiling or bundling.

148399.8k1](/packages/tonysm-importmap-laravel)[bensampo/laravel-embed

Painless responsive embeds for videos, slideshows and more.

142146.8k](/packages/bensampo-laravel-embed)[dragon-code/pretty-routes

Pretty Routes for Laravel

10058.7k4](/packages/dragon-code-pretty-routes)[laracraft-tech/laravel-useful-additions

A collection of useful Laravel additions!

58109.4k](/packages/laracraft-tech-laravel-useful-additions)

PHPackages © 2026

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