multidimensional / cakephp-subdomains
使用路由前缀轻松将子域名添加到您的 CakePHP 应用程序中。
v1.1.0
2017-01-07 03:04 UTC
Requires
- php: >=5.5.9
- cakephp/cakephp: ~3.3
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-20 21:55:17 UTC
README
使用路由前缀轻松将子域名添加到您的 CakePHP 应用程序中。基于由 chinpei215 创建的 代码。
要求
- CakePHP 3.3+
- PHP 5.5.9+
安装
您可以使用 composer 将此插件安装到 CakePHP 应用程序中。
安装 composer 包的推荐方法是
composer require --prefer-dist multidimensional/cakephp-subdomains
设置
在终端运行以下命令以加载插件:
bin/cake plugin load Multidimensional/Subdomains -b -r
或者,您可以通过手动将以下行添加到应用程序的 config/bootstrap.php 文件中来实现:
Plugin::load('Multidimensional/Subdomains', ['bootstrap' => true, 'routes' => true]);
为了充分利用 CakePHP 的 HTML 链接生成器,我们建议将我们的自定义子域名 HTML 辅助器安装到您的主 AppView.php 文件中。
// src/View/AppView.php class AppView extends View { public function initialize() { parent::initialize(); $this->loadHelper('Html', [ 'className' => 'Multidimensional/Subdomains.SubdomainHtml' ]); } }
配置
在终端运行安装脚本命令
bin/cake SubdomainsInstall
此命令将允许您自动创建一个配置文件,其中包含插件使用的子域名列表。您也可以运行此命令以添加或删除额外的子域名。
或者,您可以在主 CakePHP 配置文件夹中创建一个 config/subdomains.php 文件
return [ 'Multidimensional/Subdomains' => ['Subdomains' => ['{SUBDOMAIN_1}', '{SUBDOMAIN_2}', /*...*/ '{SUBDOMAIN_N}'] ] ];
用法
插件将自动将您指定的子域名添加为 CakePHP 前缀。
生成链接时,包括您想使用的子域名前缀,路由器将自动使用子域名 URL 创建链接。
//Link to http://admin.example.com/articles/ $this->Html->link(['prefix'=>'admin', 'controller'=>'Articles', 'action'=>'index']);
当安装了自定义 HTML 辅助器时的高级选项。
//Link to http://example.com/articles/ $this->Html->link(['prefix'=>false, 'controller'=>'Articles', 'action'=>'index']); //Link to http://www.example.com/articles/ $this->Html->link(['prefix'=>'www', 'controller'=>'Articles', 'action'=>'index']);
注意:请尽量少用 www 前缀。如果您需要将 www 前缀添加到您的域名中,我们建议安装一个中间件插件,强制将 www 添加到域名中。在任何情况下,将 'prefix' = false 设置为最佳实践。
贡献
请通过创建分支并发送拉取请求来帮助我们项目。
我们需要帮助编写单元测试、实现 Travis-CI 以及一般性地提高代码的功能。
待办事项
- Travis CI 和 PHPUnit 测试
许可
The MIT License (MIT)
Copyright (c) 2016 multidimension.al
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.