Great question! What you’re describing is a classic case of wanting to share a “core” codebase among multiple projects, while still allowing each site to have its own customizations. Here are a few approaches you can take, along with some Laravel- and git-specific advice.
1. Use a "Core" Package
Best Practice: Extract your shared logic (controllers, models, services, views, etc.) into a Laravel package. Then, each site is its own Laravel app that requires this package via Composer.
Steps:
-
Create a Laravel Package
- Move all shared code into a package (e.g.,
mycompany/realestate-core). - Publish config files, views, etc., as needed.
- Move all shared code into a package (e.g.,
-
Require the Package in Each Site
- Each site is a separate Laravel project, with its own
.env, config, and customizations. - Require your core package in
composer.json:composer require mycompany/realestate-core
- Each site is a separate Laravel project, with its own
-
Override Views/Configs as Needed
- Laravel allows you to override package views by placing them in your app’s
resources/views/vendor/{package}directory. - Each site can have its own branding, wording, etc., while still sharing the core logic.
- Laravel allows you to override package views by placing them in your app’s
-
Update the Core Once
- When you update the core package, just run
composer updatein each site.
- When you update the core package, just run
Resources:
2. Monorepo with Git Submodules or Subtrees
If you don’t want to build a package, you can use git submodules or subtrees to share code.
Submodule Example
-
Create a Core Repo
- Place all shared code in a separate git repo.
-
Add as Submodule
- In each site repo:
git submodule add https://github.com/yourname/realestate-core.git core - You can then import or symlink code from
coreas needed.
- In each site repo:
-
Update Core
- When you update the core, you can pull the latest changes into each site.
Note: Submodules can be tricky to manage, especially for beginners.
3. Branching (Not Recommended)
You could use git branches for each site, but this quickly becomes hard to manage and is not recommended for more than a couple of sites.
4. Directory Structure (Advanced)
Some teams use a monorepo with a structure like:
/core
/site1
/site2
But this requires custom deployment scripts and is more advanced.
Summary Table
| Approach | Pros | Cons |
|---|---|---|
| Laravel Package | Clean, scalable, idiomatic | Initial setup time |
| Git Submodule | Simple for small projects | Can be tricky to manage |
| Branching | Easy to start | Hard to maintain, not scalable |
| Monorepo | Powerful, flexible | Advanced, needs scripting |
Recommendation
For Laravel, the package approach is the most robust and maintainable. It’s how Laravel itself and most large apps share code.
Example: Overriding Package Views
Suppose your package has a view at resources/views/layout.blade.php. In your site, you can override it by creating:
resources/views/vendor/realestate-core/layout.blade.php
Laravel will use your site’s version instead of the package’s.
Final Tips
- Keep all site-specific configs in
.envand custom config files. - Use the package for everything that should be shared.
- Use git and Composer to manage updates.
If you need help scaffolding a Laravel package, let me know!