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

longestdrive's avatar

Package development and relying on other packages

Hi

I'm trying to break an app I've made into modules by breaking it up into packages. I'm hoping this will make it easier to maintain the functionality and develop further - but getting confused in a couple of areas. I'm new to developing as a package and currently focussing on one piece, effectively copying and pasting existing code into the package folder and trying to get it to work. I'm using jeroen-g/laravel-packager to help with development which includes Orchestra Testbench

For now, my app until now has used the 'spatie/laravel-permissions' package to protect functionality and routes etc.

I'm now trying to test a controller in the package based on a test I had in my original whole project and working through the test failures which is mainly around setting up the test environment.

My latest error is that a config/permissions.php is not loaded. So a reliance on having that package set up properly in my package - which is where I'm starting to feel I'm doing this wrong.

I can't see a way of publishing a dependant packages config file from within the package development - equally not sure I should as this has potential to add a conflict or lack of flexibility in the final project that relies on my package.

My question then is where a package includes views and functionality that need protection through roles, should I rely on the final project to worry about that or should I use this package as I am at the moment but how do I configure for the development environment?

What's the best practice of bringing in other packages into a package to add functionality - is this a bad practice?

Thanks

0 likes
5 replies
martinbean's avatar

I'm hoping this will make it easier to maintain the functionality and develop further

@longestdrive As someone who’s been down the road before, it actually makes things harder to develop and maintain. The idea of, “Everything will be modular so it will be easier” usually makes sense in theory, but then when you go down that road, it’s anything but.

When you split things into “modules” like this, you then start having to specify and maintain dependencies of versions. So if you have five packages, and you upgrade the version of Laravel you’re using from say, 7.x to 8.x, you now have to go and update the version constraints in each of your package’s composer.json files, otherwise you‘re going to get an “unsatisfiable requirements” error the next time you run composer update from your application.

Development also becomes a pain. Instead of being able to run php artisan make:model -mf in a Laravel application to create a model and a corresponding migration and factory class, you’re now going to have to do this by hand. Also, development moves from just creating files in your application, to having to create files in a package, commit that change, then run composer update in your application to get the latest package files unless you create a convoluted development environment using symlinks. So you lose the nice utilities that Artisan offers to create stubs and develop features quickly.

So as you can, my advice to the above is: “don’t”. Because the perceived benefits actually become significant drawbacks. There’s nothing wrong with having a Laravel application with lots of files. You can group your models in folders if you want to group them by “topic”, i.e. app/Models/Blog, app/Models/Shop, etc.

longestdrive's avatar

Thanks Martin. I started down this path on the basis of trying to share the whole project between two sites, each with different branding and subtle differences.

The alternative was to have the entire project as a package or to make different branches in a repository to share the code.

At the moment I have two nearly identical repositories, I update one then later on update the other one which i don't think is the easiest way of doing this, hence the package route.

Maybe I'm overthinking?

martinbean's avatar

It sounds like you may have a use for multi-tenancy if you’re wanting to share a codebase between projects/clients?

I did the exact same journey as you: I had a CMS that I was using for multiple clients. My idea was to create a “modular” CMS that I could then create websites from, and use Packagist to pull in the modules each site needed. But it still meant I had the same number of applications to maintain and keep up to date, and now I also had individual packages I needed to develop, maintain, keep dependencies up to date, etc.

I re-did the project using a single database, multi-tenancy approach and things are much, much streamlined. I’ve got one application, then runs on a single Heroku hobby dyno yet serves dozens of websites.

longestdrive's avatar

Thanks again - I'm not familiar with that concept - some reading and learning to do!!

martinbean's avatar

No problem! Feel free to start a new question if you have any queries on anything multi tenancy-related :)

Please or to participate in this conversation.