Laravel 5.3多项目共享“逻辑模型”文件夹方案咨询
Hey there! I totally feel your pain—maintaining duplicate logic models across four separate Laravel projects is a huge time sink and a recipe for inconsistencies. Let’s walk through two solid approaches to fix this, tailored for Laravel 5.3:
Option 1: Local Shared Directory (Quick & Simple)
This is perfect if you want to get up and running fast without dealing with package repositories.
- First, create a dedicated folder outside your Laravel projects to hold your shared logic models. For example:
/path/to/your/projects/ laravel-project-1/ laravel-project-2/ laravel-shared-models/ src/ Models/ UserLogic.php OrderLogic.php # All your shared model classes here - Set up proper namespaces in your shared classes. For
UserLogic.php, start with:<?php namespace Shared\Models; class UserLogic { // Your shared logic here } - For each Laravel project, open its
composer.jsonand add the shared directory to theautoload > psr-4section:"autoload": { "classmap": [ "database" ], "psr-4": { "App\\": "app/", "Shared\\Models\\": "../laravel-shared-models/src/Models/" } } - Run
composer dump-autoloadin each project to update Composer’s autoloader. - Now you can use your shared models anywhere in your Laravel projects:
Any changes you make to theuse Shared\Models\UserLogic; $userLogic = new UserLogic(); // Use your shared logic as normallaravel-shared-modelsfolder will automatically apply to all projects the next time they run (no extra steps needed).
Option 2: Independent Composer Package (Long-Term Maintainability)
If you plan to iterate on these models, add versioning, or collaborate with other developers, turning them into a proper Composer package is the way to go.
- Initialize the package: Create a new folder for your package, then run
composer initin it. Fill in the details:- Package name (e.g.,
yourusername/shared-logic-models) - Namespace (e.g.,
Shared\Models) - Set the type to
library
- Package name (e.g.,
- Organize your code in a
src/folder, matching your namespace structure (just like the local directory approach). - If you’re hosting this as a private package (most likely), add it to a Git repository (GitHub Private, GitLab, etc.).
- For each Laravel 5.3 project, add the package repository to its
composer.json:"repositories": [ { "type": "vcs", "url": "git@your-git-host.com:yourusername/shared-logic-models.git" } ] - Install the package in each project with:
(Replacecomposer require yourusername/shared-logic-models:dev-maindev-mainwith a specific version tag like1.0.0once you start versioning.) - When you update your shared logic, push the changes to your Git repo, then run this in each Laravel project to get the latest version:
composer update yourusername/shared-logic-models
Key Notes for Laravel 5.3
- Make sure your shared models don’t rely on Laravel features introduced after 5.3 (e.g., newer Facade methods, container bindings that changed in later versions).
- If your logic needs access to Laravel services (like
DBorCache), use either constructor injection or Laravel’s Facades—they’ll work as long as the services are available in the host project. - Test changes in one project first before rolling them out to all four, to avoid breaking anything unexpectedly.
Between these two options, the local directory is great for immediate relief, while the Composer package is ideal for keeping your shared logic organized and maintainable over time.
内容的提问来源于stack exchange,提问作者Richard Wieditz

