You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 5.3多项目共享“逻辑模型”文件夹方案咨询

How to Share Reusable Logic Models Across Multiple Laravel 5.3 Projects

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.json and add the shared directory to the autoload > psr-4 section:
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Shared\\Models\\": "../laravel-shared-models/src/Models/"
        }
    }
    
  • Run composer dump-autoload in each project to update Composer’s autoloader.
  • Now you can use your shared models anywhere in your Laravel projects:
    use Shared\Models\UserLogic;
    
    $userLogic = new UserLogic();
    // Use your shared logic as normal
    
    Any changes you make to the laravel-shared-models folder 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 init in it. Fill in the details:
    • Package name (e.g., yourusername/shared-logic-models)
    • Namespace (e.g., Shared\Models)
    • Set the type to library
  • 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:
    composer require yourusername/shared-logic-models:dev-main
    
    (Replace dev-main with a specific version tag like 1.0.0 once 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 DB or Cache), 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:50:06