PHPackages                             jeroen-g/autowire - 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. jeroen-g/autowire

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

jeroen-g/autowire
=================

Autowire and configure using PHP 8 Attributes in Laravel.

3.0.0(3mo ago)2320.3k↑20.8%4[1 issues](https://github.com/Jeroen-G/autowire/issues)MITPHPPHP ^8.2CI passing

Since Jan 1Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/Jeroen-G/autowire)[ Packagist](https://packagist.org/packages/jeroen-g/autowire)[ Docs](https://github.com/jeroen-g/autowire)[ RSS](/packages/jeroen-g-autowire/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (10)Dependencies (16)Versions (15)Used By (0)

🔌 Autowire for Laravel
======================

[](#-autowire-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/66c9caca11422cdce9485a5ea3c6ef8bca4d9947062e7cd25a93ea2d35a3302c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a65726f656e2d672f6175746f776972652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/jeroen-g/autowire)[![CI](https://camo.githubusercontent.com/e370ac0eade0fd10677d59c049644d688869d42720739926a494c9eeeb9171bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f4a65726f656e2d472f6175746f776972652f63692e796d6c3f6c6162656c3d43492532464344267374796c653d666c61742d737175617265)](https://github.com/Jeroen-G/autowire/actions/workflows/ci.yml)

Autowire and configure using PHP 8 Attributes in Laravel.

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

[](#installation)

Via Composer

```
composer require jeroen-g/autowire
```

You will need the configuration file to change where it should look:

```
php artisan vendor:publish --tag=autowire.config
```

Usage
-----

[](#usage)

### Autowiring

[](#autowiring)

Are you tired of binding abstract interfaces to concrete classes all the time?

```
$this->app->bind(HelloInterface::class, WorldClass::class);
```

Use the PHP 8 attribute of this package to autowire any of your interfaces:

```
namespace App\Contracts;

use JeroenG\Autowire\Attribute\Autowire;

#[Autowire]
interface HelloInterface
{
    public function hello(): string;
}
```

The class that implements that interface does not need any changes:

```
namespace App;

use App\Contracts\HelloInterface;

class WorldClass implements HelloInterface
{
    public function hello(): string
    {
        return 'world';
    }
}
```

The Autowire package will crawl through the classes and bind the abstract interface to the concrete class. If there already is a binding in the container it will skip the autowiring.

### Tagging

[](#tagging)

If you quickly want to tag all implementations of an interface, simply add the `Tag` attribute to the interface:

```
namespace App\Contracts;

use JeroenG\Autowire\Attribute\Tag;

#[Tag('myTag')]
interface HelloInterface
{
    public function hello(): string;
}
```

All implementations will now be available under the 'myTag' tag:

```
$this->app->when(Greeting::class)
	->needs(HelloInterface::class)
	->giveTagged('myTag');
```

If no tag value is specified in the attribute, the fully-namespaced name of the class will be used as the tag:

```
namespace App\Contracts;

use JeroenG\Autowire\Attribute\Tag;

#[Tag]
interface GoodbyeInterface
{
    public function goodbye(): string;
}
```

```
$this->app->when(Greeting::class)
	->needs(GoodbyeInterface::class)
	->giveTagged(GoodbyeInterface::class);
```

### Configure

[](#configure)

Personally I like injection of dependencies over resolving them using `make()` helpers. However, that means writing binding definitions such as:

```
$this->app->when($x)->needs($y)->give($z);
```

Not anymore with the Configure attribute! Here is the WorldClass example again:

```
namespace App;

use App\Contracts\HelloInterface;

#[Configure(['$message' => 'world'])]
class WorldClass
{
    private $message;

    public function __construct($message)
    {
        $this->message = $message;
    }
}
```

In this example message is a simple string. However, it can be a reference to a configuration value or other class too! The notations of config and service definitions is the same as used in Symfony.

```
// Will get the value set in config/app.php
#[Configure(['$message' => '%app.message%'])]

// Will inject an instance of the Message class
#[Configure(['$message' => '@App\Domain\Message'])]

// When you want tagged classes
#[Configure(['$messages' => '#messages'])]

// When you have multiple constructor arguments
#[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]
```

### Listen to events

[](#listen-to-events)

If you use a lot of events, the `EventServiceProvider` will very likely become long and messy:

```
protected $listen = [
    Registered::class => [
        UpdateLastLogin::class,
        ...
    ],
    ...
];
```

With the PHP 8 attribute of this package you can define the events for a listener alongside each other:

```
#[Listen(Registered::class)]
#[Listen(Login::class)]
class UpdateLastLoginListener {
   ...
}
```

The package will crawl through the classes and bind the listeners to the event classes.

### Caching

[](#caching)

The autowiring, configuration and listeners can be cached with the command `php artisan autowire:cache`. In a similar fashion it can be cleared with `php artisan autowire:clear`. Keep in mind that caching means that it won't crawl all the classes and changes to the annotations will not be loaded.

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

[](#configuration)

The package's configuration can be found in `config/autowire.php`. It should contain the list of directories where Autowire should look for both interfaces and implementations.

Custom attributes
-----------------

[](#custom-attributes)

It is possible to use custom Attribute classes for either or both the autowiring or configuration functionality:

- Create a custom attribute class, making sure to implement either `JeroenG\Autowire\Attribute\AutowireInterface` or `JeroenG\Autowire\Attribute\ConfigureInterface`, depending on the attribute you want to replace.
- Add a `autowire_attribute`, `configure_attribute`, `tag_attribute` or `listen_attribute` setting to the `config/autowire.php` file, containing the fully-namespaced name of your custom attribute class.
- Use your custom attribute to mark the interface or class you want to autowire or configure.

Speeding up tests
-----------------

[](#speeding-up-tests)

To avoid tests crawling your codebase for autowiring attributes, and thus slowing them down, you can use the `WithCachedAutowire` trait. This will make sure Autowire will only crawl once for the whole run of the tests.

```
abstract class TestCase extends BaseTestCase
{
    use WithCachedAutowire;
}
```

Changelog
---------

[](#changelog)

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

Credits
-------

[](#credits)

- [Jeroen](https://github.com/jeroen-g)
- [All Contributors](../../contributors)

License
-------

[](#license)

MIT. Please see the [license file](license.md) for more information.

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance82

Actively maintained with recent releases

Popularity37

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 66.3% 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 ~119 days

Recently: every ~276 days

Total

14

Last Release

94d ago

Major Versions

1.9.0 → 2.0.02025-08-27

2.0.0 → 3.0.02026-03-31

PHP version history (4 changes)1.0.0PHP 8.0.\*||8.1.\*

1.7.0PHP 8.0.\*||8.1.\*||8.2.\*

1.8.0PHP ^8.0

3.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/41971?v=4)[Jeroen Goudsmit](/maintainers/JeroenG)[@jeroeng](https://github.com/jeroeng)

---

Top Contributors

[![Jeroen-G](https://avatars.githubusercontent.com/u/1116853?v=4)](https://github.com/Jeroen-G "Jeroen-G (59 commits)")[![Xammie](https://avatars.githubusercontent.com/u/13081809?v=4)](https://github.com/Xammie "Xammie (15 commits)")[![BJScharp](https://avatars.githubusercontent.com/u/82942240?v=4)](https://github.com/BJScharp "BJScharp (10 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")[![bitwise-operators](https://avatars.githubusercontent.com/u/67020940?v=4)](https://github.com/bitwise-operators "bitwise-operators (1 commits)")[![niladam](https://avatars.githubusercontent.com/u/4151765?v=4)](https://github.com/niladam "niladam (1 commits)")[![svenluijten](https://avatars.githubusercontent.com/u/11269635?v=4)](https://github.com/svenluijten "svenluijten (1 commits)")

---

Tags

autowirelaravellaravelautowire

###  Code Quality

TestsPHPUnit

Code StyleECS

### Embed Badge

![Health badge](/badges/jeroen-g-autowire/health.svg)

```
[![Health](https://phpackages.com/badges/jeroen-g-autowire/health.svg)](https://phpackages.com/packages/jeroen-g-autowire)
```

###  Alternatives

[laravel/sail

Docker files for running a basic Laravel application.

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

The official AI SDK for Laravel.

1.0k3.2M194](/packages/laravel-ai)[livewire/flux

The official UI component library for Livewire.

9527.8M127](/packages/livewire-flux)[laravel/mcp

Rapidly build MCP servers for your Laravel applications.

77022.3M151](/packages/laravel-mcp)[propaganistas/laravel-disposable-email

Disposable email validator

6023.0M6](/packages/propaganistas-laravel-disposable-email)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M345](/packages/psalm-plugin-laravel)

PHPackages © 2026

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