Urgent.News

What's breaking now, across thousands of outlets.

Tech

Untangling Dependency Cycles in a Modular Laravel Monolith

Once the strangler fig from Part 1 is working, the new Laravel app grows fast. Each migrated route brings a feature with it: dealers, orders, invoices, stock. Teams usually organise that into modules, something like app/Modules/Orders , app/Modules/Billing , app/Modules/Inventory . For a few months it feels clean. Then someone adds a call from Billing into Orders to check a status, someone else…

Once the modular Laravel app is up and running, it tends to grow rapidly. Each migrated route often leads to new features like dealers, orders, invoices, and stock, which are usually organized into separate modules such as app/Modules/Orders, app/Modules/Billing, and app/Modules/Inventory. Initially, this modular structure feels clean and manageable.

However, over time, as more and more modules start calling each other, dependency cycles begin to emerge. For instance, some modules may start importing functions or classes from other modules, leading to a situation where every module ends up importing every other module as well. This situation can quickly transform the monolith into a tangled ball of mud, where changes in one module can have unintended consequences in another module.

This is where dependency cycles become problematic. A cycle between two modules means that neither module can be independently changed, tested, or extracted. It results in tests that require half of the application to be booted, changes that ripple through multiple modules, and the impossibility of extracting modules into separate services.

The solution to these dependency cycles is not to completely eliminate dependencies, but rather to create a dependency graph that flows in one direction, ensuring that lower modules do not depend on higher modules. To identify these cycles in an existing Laravel modular app, you can use a tool like deptrac. By creating a deptrac.yaml file that defines layers for each module and specifies which modules have dependencies on others, deptrac will analyze the codebase and report every dependency violation. A minimal config for a modular Laravel app might look like this:

```yaml

deptrac:

paths:

- ./app/Modules

layers:

- name: Orders

collectors:

- type: directory

value: app/Modules/Orders/.*

- name: Billing

collectors:

- type: directory

value: app/Modules/Billing/.*

- name: Inventory

collectors:

- type: directory

value: app/Modules/Inventory/.*

- name: Shared

collectors:

- type: directory

value: app/Modules/Shared/.*

ruleset:

Orders:

- Inventory

- Shared

Billing:

- Shared

Inventory:

- Shared

Shared:

- []

```

Running `vendor/bin/deptrac analyse` on this config will list every violation, providing a clear picture of the current dependency graph. While it is common to find dozens of violations in an older codebase, it is important not to try to fix them all at once. Instead, you should record the current state as a baseline using `deptrac analyse --formatter=baseline` so that your CI pipeline only flags new violations while you work through the existing ones.

Once you have identified the cycles in your dependency graph, the next step is to decide the direction for each relationship. My rule of thumb is to determine which module would still make sense if the other one disappeared. For example, Billing might exist without knowing about order screens, but Orders cannot place an order without a price.

In this case, Orders would be considered the lower module, and Billing the higher module. The third step is to break each reverse edge. There are three techniques that can be used, depending on the situation: 1. Contract (interface): If the lower module needs to call the higher module synchronously for data, you can define a contract interface in the higher module and implement it in the lower module.

This maintains a runtime link, but inverts the direction of the dependency. 2. Domain event: If the lower module only needs to react after something happens in the higher module, you can use a domain event. This allows for eventual consistency and requires idempotent listeners. 3. Anti-corruption layer: If a module keeps reaching into legacy tables or another module's models, you can create an anti-corruption layer that provides extra mapping code to isolate the mess.

Regardless of the technique chosen, it is essential to fail the CI pipeline on any new dependency violations, ensuring that the dependency graph remains one-directional and preventing the recurrence of dependency cycles.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

SCIM Provisioning & Deprovisioning That Actually Removes Access

Here is a scenario I now test on every enterprise SSO integration. An employee leaves a customer company. Their IT team disables the account in the identity provider.

  • IdP sends deprovisioning request with user's unique identifier
  • Application blocks login, revokes live sessions, disables tokens
  • Transaction-safe step ensures single-step deprovisioning

Tell us your story, and we might read it on the CodeNewbie Podcast 🎙️

Hi! I’m Lauren 👩🏼💻, the new host of the CodeNewbie Podcast , and I’d love to hear your story. This season, I’m asking for your help.

  • Lauren announces hosting CodeNewbie Podcast season
  • Encourages sharing coding stories, triumphs, challenges
  • Interested in AI-related experiences and lessons learned

Keeping secrets out of agent prompts: how Vault and DLP fit together

Originally published on sandscale.dev . An agent that deploys code, queries a database or calls an API needs credentials.

  • Credentials can leak in agent runs through prompts, files, or tool-call arguments.
  • Leaked credentials appear in provider logs, user transcripts, and model context.
  • Vault and DLP solutions help secure secrets and prevent accidental exposure.

More from Friday 20 November →