Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shadrix's avatar
Level 12

Your thoughts about writing everything in modules/packages?

Hi guys,

so my app gets quite huge and it is quite difficult to have a good overview.

What do you think, is writing your code in modules a good thing? I just stumbled upon this: https://github.com/nWidart/laravel-modules

I had this thought a long time ago when I saw how Illuminate is written. It is separated in several "modules" that are independent.

Now I wonder if you guys are using this technique in your big projects as well.

Before I found this laravel-modules package, I thought to myself, perhaps you could create for every module an own repository.

Not sure if there are some cons to it.

Thanks & greetings from Germany!

0 likes
3 replies
artcore's avatar
artcore
Best Answer
Level 5

I'm doing the same thing, the only way to make portable apps. As a tip, if you don't want a dependency like laravel-modules you can create your own composer.json in your folder structure that contains the whole app and add it to laravel's composer with a symlink path

Here's an example of a personal package composer.json

{
  "name": "extend/utility",
  "version": "1.0.0.1",
  "description": "Utility package Laravel",
  "type": "library",
  "license": "MIT",
  "authors": [
    {
      "name": "Albert",
      "email": ""
    }
  ],
  "autoload": {
    "psr-4": {
      "Extend\Utility\": "/"
    }
  },
  "extra": {
    "laravel": {
      "providers": [
        "Extend\Utility\Foundation\ServiceProvider"
      ],
      "aliases": {}
    }
  },
  "minimum-stability": "dev",
  "prefer-stable": true
}

and the laravel composer entries

{
  "name": "laravel/laravel",
  "type": "project",
  "description": "The Laravel Framework.",
  "keywords": [
    "framework",
    "laravel"
  ],
  "license": "MIT",
  "require": {
    "php": "^7.1.3",
    "fideloper/proxy": "^4.0",
    "laravel/framework": "5.7.*",
    "extend/utility": "1.0.*",
    "ext-pdo": "*"
  },
  "require-dev": {
    "beyondcode/laravel-dump-server": "^1.0",
    "filp/whoops": "^2.0",
    "fzaninotto/faker": "^1.4",
    "mockery/mockery": "^1.0",
    "nunomaduro/collision": "^2.0",
    "phpunit/phpunit": "^7.0"
  },
  "config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "sort-packages": true
  },
  "extra": {
    "laravel": {
      "dont-discover": []
    }
  },
  "autoload": {
    "psr-4": {
      "App\": "app/"
    },
    "classmap": [
      "database/seeds",
      "database/factories"
    ]
  },
  "autoload-dev": {
    "psr-4": {
      "Tests\": "tests/"
    }
  },
  "minimum-stability": "dev",
  "prefer-stable": true,
  "scripts": {
    "post-autoload-dump": [
      "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
      "@php artisan package:discover --ansi"
    ],
    "post-root-package-install": [
      "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
      "@php artisan key:generate --ansi"
    ]
  },
  "repositories": [
    {
      "type": "path",
      "url": "./Extend/Utility/"
    }
  ]
}

update composer and you're good to go ;)

edit: forgot to mention you need to hook into Laravel I'm using my own service provider that registers all I need so only have to add this to Laravel's via auto discover

One example:

<?php namespace Extend\Localization\Foundation;

use Extend\Localization\Service\Console\ImportCities;
use Extend\Localization\Service\Console\ImportCitiesPostalCodes;
use Extend\Localization\Service\Console\ImportCounties;
use Extend\Localization\Service\Console\ImportCountries;
use Extend\Localization\Service\Console\ImportPostalCodes;
use Extend\Localization\Service\Console\ImportStates;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
  protected $commands =
    [
      ImportCountries::class,
      ImportStates::class,
      ImportCounties::class,
      ImportCities::class,
      ImportPostalCodes::class,
      ImportCitiesPostalCodes::class,
    ];


  public function boot()
  {
    $this->loadViewsFrom(__DIR__ . '/../resources/views', 'localization');
    $this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
    //$this->loadTranslationsFrom(__DIR__.'/../translations', 'localization');
  }


  public function register()
  {
    $this->mergeConfigFrom(__DIR__ . '/../config.php', 'localization');
    $this->commands($this->commands);

//    $this->app->register(RouteServiceProvider::class);
  }
}
1 like
shadrix's avatar
Level 12

@artcore thank you! And the tip with symlink is awesome. Actually, when I think about it, Laravel Nova is doing the same.

artcore's avatar

My pleasure! Laravel is making development too awesome especially since it doesn't force you to work their way

1 like

Please or to participate in this conversation.