{
  "id": 20921284,
  "title": "Untangling Dependency Cycles in a Modular Laravel Monolith",
  "url": "https://urgent.news/2026/11/20/untangling-dependency-cycles-in-a-modular-laravel-monolith",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-20T16:29:37.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/muhammadalimughal/untangling-dependency-cycles-in-a-modular-laravel-monolith-nl7"
  },
  "original_language": "en",
  "account": "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:\n\n```yaml\ndeptrac:\npaths:\n- ./app/Modules\nlayers:\n- name: Orders\ncollectors:\n- type: directory\nvalue: app/Modules/Orders/.*\n- name: Billing\ncollectors:\n- type: directory\nvalue: app/Modules/Billing/.*\n- name: Inventory\ncollectors:\n- type: directory\nvalue: app/Modules/Inventory/.*\n- name: Shared\ncollectors:\n- type: directory\nvalue: app/Modules/Shared/.*\nruleset:\nOrders:\n- Inventory\n- Shared\nBilling:\n- Shared\nInventory:\n- Shared\nShared:\n- []\n```\n\nRunning `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.",
  "summary": "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…",
  "key_points": [
    "Laravel modular app grows rapidly with new features in separate modules",
    "Dependency cycles emerge as modules import functions from each other",
    "deptrac tool identifies dependency violations in codebase"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}