单个Laravel实例部署多站点是否具备可行性?
Absolutely! Running multiple sites from a single Laravel instance is not only feasible—it’s a perfect fit for your use case where you want shared data and a unified API, but need distinct branding and some unique pages per site. You don’t have to set up separate Laravel installations; here’s how to pull this off:
1. Domain-Based Routing
Use Laravel’s Route::domain() method to group routes by your sites’ domains. This lets you define site-specific routes while keeping your shared API accessible across all domains:
// routes/web.php // Site 1 routes Route::domain('site1.com')->group(function () { Route::get('/', [Site1Controller::class, 'showHome']); Route::get('/about', [Site1Controller::class, 'showAbout']); // Add other Site 1-specific pages }); // Site 2 routes Route::domain('site2.com')->group(function () { Route::get('/', [Site2Controller::class, 'showHome']); Route::get('/contact', [Site2Controller::class, 'showContact']); // Add other Site 2-specific pages }); // Shared API routes (accessible from both domains) Route::prefix('api')->group(function () { Route::get('/products', [ApiController::class, 'getProducts']); Route::post('/orders', [ApiController::class, 'createOrder']); // All shared API endpoints go here });
2. Branding & View Customization
Create separate view directories for each site to handle unique layouts, branding, and pages, while still pulling shared data from your database:
- Make directories like
resources/views/site1andresources/views/site2 - Load the appropriate view in each site’s controller:
// app/Http/Controllers/Site1Controller.php public function showHome() { // Fetch shared data (same for both sites) $featuredProducts = Product::where('featured', true)->get(); // Load Site 1's home view return view('site1.home', compact('featuredProducts')); } // app/Http/Controllers/Site2Controller.php public function showHome() { // Same shared data, different view $featuredProducts = Product::where('featured', true)->get(); return view('site2.home', compact('featuredProducts')); }
To avoid repeating brand-related variables (like logos, colors) across views, use a view composer to inject site-specific data into all views:
// app/Providers/AppServiceProvider.php public function boot() { View::composer('*', function ($view) { $domain = request()->getHost(); $brand = match($domain) { 'site1.com' => [ 'logo' => 'site1-logo.png', 'primaryColor' => '#e63946', 'siteName' => 'Site One' ], 'site2.com' => [ 'logo' => 'site2-logo.png', 'primaryColor' => '#457b9d', 'siteName' => 'Site Two' ], }; $view->with('brand', $brand); }); }
Then reference these variables in your views:
{{-- resources/views/site1/home.blade.php --}} <header> <img src="{{ asset('site1/' . $brand['logo']) }}" alt="{{ $brand['siteName'] }}"> <style> .primary-btn { background-color: {{ $brand['primaryColor'] }}; } </style> </header>
3. Site-Specific Configuration
If you need unique settings per site (like mail drivers, third-party API keys), you can:
- Create site-specific config files (e.g.,
config/site1.php,config/site2.php) - Dynamically load the config based on the current domain:
// Fetch Site 1's mail config $siteConfig = config('site1.mail');
Or use prefixed environment variables in your .env file:
SITE1_MAIL_DRIVER=smtp SITE1_MAIL_HOST=smtp.site1.com SITE2_MAIL_DRIVER=mailgun SITE2_MAIL_HOST=smtp.mailgun.org
Then map these to your config files:
// config/mail.php 'driver' => match(request()->getHost()) { 'site1.com' => env('SITE1_MAIL_DRIVER', 'smtp'), 'site2.com' => env('SITE2_MAIL_DRIVER', 'smtp'), },
4. Static Assets Organization
Store site-specific static files (images, CSS, JS) in subdirectories under public/:
public/site1/css/,public/site1/images/public/site2/css/,public/site2/images/
Reference them in your views using asset():
<link rel="stylesheet" href="{{ asset('site1/css/main.css') }}">
When Should You Use Separate Instances?
You’d only need independent Laravel sites if:
- Each site requires completely different dependencies or Laravel versions
- The business logic for each site becomes so distinct that sharing code leads to unmanageable coupling
- You need full isolation for security or compliance reasons
For your use case (shared API/data, just branding/page differences), a single Laravel instance is clean, efficient, and easy to maintain.
内容的提问来源于stack exchange,提问作者Eric Goncalves

