/var/www/
├── shared-template/
│ ├── bootstrap.php
│ ├── template.php
│ ├── functions.php
│ └── assets/
│
├── site1.pl/
│ └── public/
│ └── index.php
│
├── site2.pl/
│ └── public/
│ └── index.php
│
└── site3.pl/
└── public/
└── index.php
W site1.pl/public/index.php:
<?php
$config = [
'site_name' => 'Site 1',
'domain' => 'site1.pl',
'title' => 'Strona Site 1',
'theme' => 'blue',
'contact' => [
'email' => 'kontakt@site1.pl',
'phone' => '+48 123 456 789',
],
];
require '/var/www/shared-template/bootstrap.php';
Dla drugiego serwisu:
<?php
$config = [
'site_name' => 'Site 2',
'domain' => 'site2.pl',
'title' => 'Strona Site 2',
'theme' => 'green',
'contact' => [
'email' => 'biuro@site2.pl',
'phone' => '+48 987 654 321',
],
];
require '/var/www/shared-template/bootstrap.php';
A wspólny bootstrap.php:
<?php
if (!isset($config) || !is_array($config)) {
throw new RuntimeException('Brak konfiguracji strony.');
}
require_once __DIR__ . '/functions.php';
require __DIR__ . '/template.php';
W template.php możesz bezpośrednio korzystać z konfiguracji:
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<title><?= htmlspecialchars($config['title']) ?></title>
</head>
<body class="theme-<?= htmlspecialchars($config['theme']) ?>">
<header>
<h1><?= htmlspecialchars($config['site_name']) ?></h1>
</header>
<main>
<p>
Kontakt:
<a href="mailto:<?= htmlspecialchars($config['contact']['email']) ?>">
<?= htmlspecialchars($config['contact']['email']) ?>
</a>
</p>
</main>
</body>
</html>
Ja zrobiłbym jednak jeszcze jedną rzecz: oddzielił konfigurację od index.php. Wtedy każdy site ma:
site1.pl/
├── config.php
└── public/
└── index.php
config.php:
<?php
return [
'site_name' => 'Site 1',
'domain' => 'site1.pl',
'title' => 'Strona Site 1',
'theme' => 'blue',
'contact' => [
'email' => 'kontakt@site1.pl',
],
];
A index.php staje się praktycznie identyczny dla wszystkich stron:
<?php
$config = require dirname(__DIR__) . '/config.php';
require '/var/www/shared-template/bootstrap.php';
To ma kilka zalet: wspólny template aktualizujesz tylko raz, każdy site ma własne ustawienia, możesz trzymać config.php poza DocumentRoot, a index.php jest bardzo prosty.
Jeśli strony mają również różne treści, można rozwinąć config np. tak:
return [
'site_name' => 'Site 1',
'seo' => [
'title' => 'Site 1 - usługi',
'description' => 'Opis strony Site 1',
],
'hero' => [
'title' => 'Witamy w Site 1',
'text' => 'Treść zależna od konkretnego serwisu.',
],
'colors' => [
'primary' => '#0055aa',
'secondary' => '#eeeeee',
],
];
i jeden template obsłuży dziesiątki domen.
Jeżeli chcesz, mogę też pokazać wariant, który polecam przy 20–100 domenach, gdzie masz jeden wspólny kod PHP, a konfiguracja jest automatycznie wybierana na podstawie $_SERVER['HTTP_HOST'].