buildrr / multisite
为 CakePHP 3.x 开发的多站插件,允许您从单个 CakePHP 安装中托管多个站点。
Requires
- php: >=5.4.0
- cakephp/cakephp: ~3.2
This package is auto-updated.
Last update: 2024-09-27 02:01:57 UTC
README
一个插件,允许您拥有一个支持多个网站的 Cakephp 3.x 应用程序,同时足够灵活,您可以按站点定制单个文件。
例如:如果请求 example.com,您可能有一些文件,如 APP/sites/example.com/config/app.php
和 APP/sites/example.com/vendor/CakeDC/Users/src/Controllers/UsersController.php
,这两个文件都会根据每个站点的需要覆盖核心 APP 中的相应默认文件。
安装
在控制台运行
composer require buildrr/multisite
将 APP/config/bootstrap.php 的前 6 行替换为以下内容。
<?php /** * Configure paths required to find CakePHP + general filepath * constants */ require __DIR__ . '/paths.php'; /** * Get multisites */ require ROOT . DS . 'sites' . DS . 'bootstrap.php';
找到 Configure::config('default', new PhpConfig());
并替换为
$__config_path = false;
if(defined('SITE_DIR')) $__config_path = SITE_DIR . DS . 'config' . DS;
Configure::config('default', new PhpConfig($__config_path));
在 APP/composer.json 中添加 ```"BuildrrMultiSite\Console\AutoLoader::postAutoloadDump"到
"post-autoload-dump"`` 如此
"scripts": { "post-install-cmd": "App\\Console\\Installer::postInstall", "post-autoload-dump": [ "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump", "BuildrrMultiSite\\Console\\AutoLoader::postAutoloadDump" ] },
在 SITE_DIR/config/app.php 中设置 App.paths.templates 如此
'App' => [ 'paths' => [ 'templates' => [ ROOT . DS . SITE_DIR . DS . 'vendor' . DS . '%s' . DS . 'src' . DS . 'Template', ROOT . DS . SITE_DIR . DS . 'src' . DS . 'Template' . DS, APP . 'Template' . DS, CORE_PATH . 'src'. DS . 'Template' . DS ], ], ],
在 APP/src/View/AppView.php 添加以下两个
// near the top of the file, outside of class AppView() use BuildrrMultiSite\View\MultisiteView; // inside of class AppView() /** * Overwrites Cake/View::_paths() * * @param null $plugin * @param bool $cached * @return mixed */ protected function _paths($plugin = null, $cached = true) { $multisite = new MultisiteView(); $multisite->theme = $this->theme; return $multisite->_paths($plugin, $cached); }
用法
- 在 APP/sites/example.com 和 APP/sites/example-app-folder 创建一个文件夹。
- 在这些文件夹中,您可以创建核心 APP 文件的定制版本。
- 例. APP/sites/example.com/config/app.php (在这里创建自己的数据库连接)
- 例. APP/sites/example-app-folder/config/app.php (在这里为该站点创建不同的数据库连接)
在 APP/sites/bootstrap.php 创建一个文件夹和文件 (这些都是示例,将名称更改为您实际想要使用的域名)
<?php /** * Map domains this install will support, to where that site's files are located. * The incoming request domain is on the left, the folder within the sites * directory that the request maps to is on the right. */ $domains['example.com'] = 'example.com'; $domains['example-app.com'] = 'example-app-folder'; /** * find the folder that is being requested by the domain */ if (!empty($domains[$_SERVER['HTTP_HOST']])) { if (!defined('SITE_DIR')) { // this is the site combined local and remote sites directory define('SITE_DIR', 'sites/' . $domains[$_SERVER['HTTP_HOST']]); } }
如果您想为个别站点提供可以定制或覆盖的插件,您需要将其添加到主 APP/composer.json
文件中的自动加载参数。格式为 "VendorName\\PluginName\\": "./SITE_DIR/vendor/[vendor name]/[plugin name]/src"
,例如...
// APP/composer.json "autoload": { "psr-4": { "App\\": "src", "BuildrrMultiSite\\": "./SITE_DIR/vendor/buildrr/multisite/src" } },
每次您向此自动加载添加新插件时,都需要在控制台运行此命令
composer dump-autoload