PHPackages                             a2workspace/laravel-automount - 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. a2workspace/laravel-automount

ActiveLibrary

a2workspace/laravel-automount
=============================

讓依賴注入更為簡單

v1.1.0(3y ago)7105[1 issues](https://github.com/A2Workspace/laravel-automount/issues)MITPHPPHP ^7.4|~8.0

Since May 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/A2Workspace/laravel-automount)[ Packagist](https://packagist.org/packages/a2workspace/laravel-automount)[ RSS](/packages/a2workspace-laravel-automount/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (0)

Laravel Automount
=================

[](#laravel-automount)

[ ![](https://github.com/A2Workspace/laravel-automount/actions/workflows/coverage.yml/badge.svg)](https://github.com/A2Workspace/laravel-automount)[ ![](https://camo.githubusercontent.com/427a653d0b2ab411fc98dc7d58cd3ac74287bd2825afe987a7c4dc291b383717/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f4132576f726b73706163652f6c61726176656c2d6175746f6d6f756e742f74657374733f7374796c653d666c61742d737175617265)](https://github.com/A2Workspace/laravel-automount)[ ![](https://camo.githubusercontent.com/895244b5a6a75c7beb024690e038f496deab95cb78e0876cfdcfb3c0e8558497/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f4132576f726b73706163652f6c61726176656c2d6175746f6d6f756e742e7376673f7374796c653d666c61742d737175617265)](https://codecov.io/gh/A2Workspace/laravel-automount)[ ![](https://camo.githubusercontent.com/95d1ff61e082c31b6e84332279b133cb15f96d9ed8d9a798bdd9e176381c2f9b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f4132576f726b73706163652f6c61726176656c2d6175746f6d6f756e743f7374796c653d666c61742d737175617265)](https://github.com/A2Workspace/laravel-automount/blob/master/LICENSE)[ ![](https://camo.githubusercontent.com/5d2593a844e3983208075d67adcf0290b43d66eec505ce4ff898a33d8dc2595f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6132776f726b73706163652f6c61726176656c2d6175746f6d6f756e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/a2workspace/laravel-automount)[ ![](https://camo.githubusercontent.com/ad45d4072dbbd64d5403d2d75d46cba4fcf479788366b49549de1716bc652f46/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6132776f726b73706163652f6c61726176656c2d6175746f6d6f756e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/a2workspace/laravel-automount)

**隨著 PHP 8 的新特性增加，你也許已經不需要此套件了，可參考 [Constructor Property Promotion](https://www.php.net/releases/8.0/en.php#constructor-property-promotion) 的作法**

Laravel 的依賴注入很棒，但我們讓他更好！

- [關於](##%E9%97%9C%E6%96%BC)
- [安裝](##%E5%AE%89%E8%A3%9D)
- [如何使用](##%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8)
    - [不會被處理的屬性](##%E4%B8%8D%E6%9C%83%E8%A2%AB%E8%99%95%E7%90%86%E7%9A%84%E5%B1%AC%E6%80%A7)
- [限制](##%E9%99%90%E5%88%B6)
    - [覆寫建構子](##%E8%A6%86%E5%AF%AB%E5%BB%BA%E6%A7%8B%E5%AD%90)
    - [避免私有屬性](##%E9%81%BF%E5%85%8D%E7%A7%81%E6%9C%89%E5%B1%AC%E6%80%A7)

關於
--

[](#關於)

想想看，當你專案越來越大，一定有遇過又臭又長的建構子：

```
use A2Workspace\AutoMount\AutoMountDependencies;

class ProductController extends Controller
{
    protected ProductService $productService;

    protected ProductRepository $productRepository;

    protected ProductStockManager $productStockManager;

    public function __construct(
        ProductService $productService,
        ProductRepository $productRepository,
        ProductStockManager $productStockManager
    ) {
        $this->productService = $productService;
        $this->productRepository = $productRepository;
        $this->productStockManager = $productStockManager;
    }

    public function __invoke(Request $request)
    {
        $this->productService->doSomething();
    }
}
```

透過 `AutoMountDependencies` 特性自動掛載依賴，讓我們省略繁雜的綁定過程。

```
use A2Workspace\AutoMount\AutoMountDependencies;

class ProductController extends Controller
{
    use AutoMountDependencies; // Add this.

    protected ProductService $productService;

    protected ProductRepository $productRepository;

    protected ProductStockManager $productStockManager;

    // We don't need the constructor anymore...

    public function __invoke(Request $request)
    {
        $this->productService->doSomething(); // Still works!
    }
}
```

安裝
--

[](#安裝)

此套件基於 PHP 7.4 的 [Typed Properties](https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.typed-properties)新功能，請確保你的 PHP 更新到最新版本。

```
composer require a2workspace/laravel-automount
```

如何使用
----

[](#如何使用)

僅須在目標類別上使用 `AutoMountDependencies` 特性:

```
use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;
}
```

接著，有型別定義的類別屬性就會在建構時自動做依賴注入。

```
use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;

    protected PaymentGatewayFactory $factory;
}
```

**注意**: 考慮到繼承，請避免使用 `private` 私有在要被處理的屬性上。

### 不會被處理的屬性

[](#不會被處理的屬性)

以下類型的屬性會被略過處理:

1. 基本型別 (int, float, bool, array ...)
2. 未定義型別的屬性
3. 定義為 [Nullable](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.nullable) 的屬性
4. 有初始值的屬性

```
use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;

    protected int $amount; // Pass.

    protected array $gateways; // Pass.

    protected $something; // Pass.

    protected ?PaymentGateway $gateway = null; // Pass.

    protected PaymentGatewayFactory $factory; // Do inject.
}
```

限制
--

[](#限制)

### 覆寫建構子

[](#覆寫建構子)

`AutoMountDependencies` 特性中定義了在建構子中執行依賴掛載的動作。當你需要覆寫 (Override) `__construct()` 時，記得手動呼叫 `mountDependencies()` 方法。

```
use A2Workspace\AutoMount\AutoMountDependencies;

class PaymentService
{
    use AutoMountDependencies;

    public function __construct()
    {
        $this->mountDependencies(); // Add this line.

        // ...
    }
}
```

### 避免私有屬性

[](#避免私有屬性)

當型別屬性被定義為私有，會導致繼承後的子類別，在建構時無法寫入屬性而造成錯誤。

請謹慎使用 `private` ，或將屬性定義為 [Nullable](https://www.php.net/manual/en/language.types.declarations.php#language.types.declarations.nullable) 。

```
use A2Workspace\AutoMount\AutoMountDependencies;

abstract class BasePaymentGateway
{
    use AutoMountDependencies;

    private PaymentServiceProvider $service;
}

class PaymentGateway extends BasePaymentGateway
{
    // ...
}

$gateway = new PaymentGateway; // 拋出 PaymentGateway::$service 未被初始之錯誤
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance17

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

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 ~157 days

Total

4

Last Release

1346d ago

PHP version history (2 changes)v1.0.0PHP ^7.4|^8.0

v1.1.0PHP ^7.4|~8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3df1b6bbe4567dd2a69769a4902e86f1e479065d5c5cf395f8104dde6f08deb3?d=identicon)[Shishamou](/maintainers/Shishamou)

---

Top Contributors

[![Shishamou](https://avatars.githubusercontent.com/u/7775781?v=4)](https://github.com/Shishamou "Shishamou (13 commits)")

---

Tags

laravelphplaravel

### Embed Badge

![Health badge](/badges/a2workspace-laravel-automount/health.svg)

```
[![Health](https://phpackages.com/badges/a2workspace-laravel-automount/health.svg)](https://phpackages.com/packages/a2workspace-laravel-automount)
```

###  Alternatives

[rtconner/laravel-likeable

Trait for Laravel Eloquent models to allow easy implementation of a 'like' or 'favorite' or 'remember' feature.

394388.0k5](/packages/rtconner-laravel-likeable)[hemp/presenter

Easy Model Presenters in Laravel

247592.6k1](/packages/hemp-presenter)[slowlyo/owl-admin

基于 laravel、amis 开发的后台框架~

61214.2k26](/packages/slowlyo-owl-admin)

PHPackages © 2026

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