Start a Project

Bagisto’s Proxy Pattern: Override Core Models Without Forking

The Bagisto Proxy Pattern is one of the framework’s most powerful extension mechanisms.

It lets developers override core models without editing vendor files or forking the platform.

You might add a method to the Product model, modify a relationship, or replace an entire model with a custom implementation.

In most frameworks that means editing vendor files (wiped on the next update) or forking the platform (which you then maintain forever).

Bagisto Dev Doc solves this with the Proxy Pattern.

It adds a thin layer of indirection that lets any package replace a core model at runtime.

No changes to the core code are required.

  • Think of a restaurant that never hardcodes the chef on its menu. Instead, it simply says “prepared by the chef on duty.”
  • Swap the person behind the pass, and every dish is now cooked by the new chef. There is no need to reprint the menu.
  • Bagisto’s proxies work the same way. The code always asks for ProductProxy, and whoever is registered at that moment does the cooking.

Bagisto Proxy Pattern: Before vs. After

1: Without the Proxy Pattern

  • Adding one method to Product means editing core – lost on the next composer update.
  • However, relationships often hardcode Product::class
  • Overriding a model means grep-and-replacing dozens of references.

2: With Bagisto’s Proxy Pattern

  • Extend the model in your own package; register it in one line.
  • Relationships reference ProductProxy::modelClass(), resolved to whatever is registered.
  • Every reference across the codebase updates automatically – nothing to grep.

How the Bagisto Proxy Pattern Works

Bagisto sits on Konekt Concord, a Laravel modularization library.

Every model is paired with two small companions: a Contract and a Proxy.

The Contract defines the model’s interface, while the Proxy resolves that contract to the concrete model currently registered.

Application code references the proxy, never the concrete class.

Bagisto Proxy Pattern Under the Hood

1. Bagisto Proxy Pattern: The Model, the Contract, and the Proxy

The contract is just an empty interface that gives the model a stable, abstract name:

The proxy has no body at all – all the intelligence lives in the parent ModelProxy:

2. Registering the Model with Concord

Each package’s ModuleServiceProvider lists the models it owns in a single array:

On boot, Concord derives each model’s contract and binds the pair in the container:

3. Referencing the Proxy

Relationships never name a concrete class – they call ::modelClass() on the proxy:

The convention runs both directions – a wishlist points back at a product the same way:

4. How modelClass() Resolves the Real Class

5. Forwarding static calls

You can also call model methods directly on the proxy.

The __callStatic method forwards each call to the real model class.

Bagisto Proxy Pattern: Overriding a Core Model

1. Extend the core model

2. Register it against the contract

Final Thoughts : Why the Bagisto Proxy Pattern Matters

By pairing every model with a contract and a proxy, Bagisto makes model substitution simple.

What is usually a fork-or-suffer problem becomes a one-line registration.

Your customizations stay outside the core, making upgrades much easier.

You can also hire Laravel developers to build custom solutions on Laravel. To explore available extensions, check out the Bagisto Extension marketplace.

Exit mobile version