codeblastr/multisite

为 CakePHP 3.x 开发的多站插件,它允许您从单个 CakePHP 安装中管理多个站点。

安装: 15

依赖: 0

建议者: 0

安全: 0

星标: 2

关注者: 3

分支: 2

开放问题: 0

类型:cakephp-plugin

dev-master 2016-04-02 18:40 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:04:02 UTC


README

一个插件,允许您拥有一个支持多个网站的单一 Cakephp 3.x 应用程序,同时足够灵活,可以按站点定制个别文件。

例如:如果请求 example.com,您可能有一些文件如 APP/sites/example.com/config/app.phpAPP/sites/example.com/vendor/CakeDC/Users/src/Controllers/UsersController.php,这两个文件都会在每个站点的核心 APP 中覆盖相应的默认文件。

安装

在控制台中运行

composer require codeblastr/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';

在 APP/composer.json 中添加 ```"CodeBlastrMultiSite\Console\AutoLoader::postAutoloadDump""post-autoload-dump"``,如下所示

"scripts": {
    "post-install-cmd": "App\\Console\\Installer::postInstall",
    "post-autoload-dump": [
        "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump",
        "CodeBlastrMultiSite\\Console\\AutoLoader::postAutoloadDump"
    ]
},

在 APP/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 CodeBlastrMultiSite\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/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",
        "CodeBlastrMultiSite\\": "./SITE_DIR/vendor/codeblastr/multisite/src"
    }
},

每次您向自动加载中添加新的插件时,都需要在控制台中运行此命令

composer dump-autoload