Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to organise composer and configuration in modules #1170

Closed
moetanahy opened this issue Jan 25, 2021 · 3 comments
Closed

How to organise composer and configuration in modules #1170

moetanahy opened this issue Jan 25, 2021 · 3 comments
Labels

Comments

@moetanahy
Copy link

Just to start off, i think the package is excellent and is really helping me understand how to properly modularise my code in Laravel - great stuff!

I had a few questions with composer and the configuration of sub-modules and their dependency packages.

I'm using the module Media as an example to explain my problem.

(1) Composer

I have a module which uses some of it's own dependencies - I put them in the Module/Media/composer.json. I then when building my project run it as follows - for example - spatie/laravel-medialibrary. I am importing it in the Media/composer.json - I then run

cd Media
composer install
cd ..
composer install

I am then loading this, but a Trait then needs to be applied within the User model object which is in the root module - and it is erroring out as can't find the Spatie package above - does that mean I have to add the dependency in the parent as well as the Media package? Is there anyway to force the root project to actually "import" the composer dependencies in the sub-modules to make life easier? Is there something else I'm missing here?

(2) Configuration

As part of the Media sub-module I'm creating, it uses the spatie/laravel-medialibrary package which publishes a config file called media-library.php. I have put this within the Media/Config/ folder to keep all the conifigurtion related to the Media library in one place. Should this be how I specify it or should this be in the /rootproject/config directory?

I tried putting this in the Media/config directory then publishing this using

php artisan module:publish-config Media

but this exports the config for the Media module, and not the one exported from the external Spatie library

Am i doing something wrong here?

(3) Database Migrations

For the deployment process to a production environment, shoudl I be publishing the migrations from sub-modules to the root project or just running php artisan migrate to "in theory" run all migrations?

Any help here would be much appreciated.

Thanks in advance!

Moe

@jaimyborgman
Copy link

I'm having the same problem with loading dependencies from modules, on the module side it's working fine but it's not auto-discovering those dependencies even while manually registering those will give an error on the higher project level.

So what would be the best solution? I'll understand that laravel is having its own auto discovering process but if that's now conflicting what would be the best way to add dependencies for modules in the total project?

@elceka
Copy link

elceka commented Mar 22, 2021

(1) Composer

You can extend User model from Main application in your's module and then implement necessary trait.

(2) Configuration

I use somethink like this in boot() method of module ServiceProvider:

public function publishConfig(): void
{
    $configPathTerminy = __DIR__ . '/../config/terminy.php';
    $configPathGeocoder = __DIR__ . '/../config/geocoder.php';

    if ($this->app->runningInConsole()) {
        $this->publishes([
            $configPathTerminy => config_path('terminy.php'),
        ], 'config');
    }

    $this->mergeConfigFrom($configPathTerminy, "terminy");
    $this->mergeConfigFrom($configPathGeocoder, "geocoder");
}

(3) Database Migrations

I define migrations in the ServiceProvider and then in Composer script of Main application:

"@php artisan vendor:publish --force --provider=\"Elceka\\Terminy\\TerminyServiceProvider\" --tag=migrations --tag=seeds --tag=assets --tag=config --ansi"

@stale
Copy link

stale bot commented Aug 3, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 3, 2021
@stale stale bot closed this as completed Aug 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants