Easily add sub domains to your CakePHP application using route prefixes

Overview

CakePHP Subdomain Routing

Build Status Latest Stable Version Code Coverage Minimum PHP Version License Total Downloads Scrutinizer Code Quality

Easily add sub domains to your CakePHP application using route prefixes. Based on code created by chinpei215.

Requirements

  • CakePHP 3.3+
  • PHP 5.5.9+

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require --prefer-dist multidimensional/cakephp-subdomains

Setup

Load the plugin by running following command in terminal:

bin/cake plugin load Multidimensional/Subdomains -b -r

Or by manually adding following line to your app's config/bootstrap.php:

Plugin::load('Multidimensional/Subdomains', ['bootstrap' => true, 'routes' => true]);

In order to get the most out of the HTML link generator in CakePHP, we recommend installing our custom Subdomain HTML helper into your master AppView.php.

// src/View/AppView.php
class AppView extends View
{
    public function initialize()
    {
        parent::initialize();
        $this->loadHelper('Html', [
            'className' => 'Multidimensional/Subdomains.SubdomainHtml'
        ]);
    }
}

Configuration

Run the installation script command in termainl:

bin/cake SubdomainsInstall

This command will allow you to automatically create a configuration file with the list of your subdomains for the plugin to use. You can also run this command to add or delete additional subdomains.

Alternatively, you can create a config/subdomains.php file in your main CakePHP config folder:

return [
    'Multidimensional/Subdomains' => 
        ['Subdomains' =>
            ['{SUBDOMAIN_1}', '{SUBDOMAIN_2}', /*...*/ '{SUBDOMAIN_N}']
        ]
    ];

Usage

The plugin will automatically add the subdomains you specify as CakePHP prefixes.

When generating links, include the subdomain prefix you want to use and the Router will automatically create the link using the subdomain URL.

//Link to http://admin.example.com/articles/
$this->Html->link(['prefix'=>'admin', 'controller'=>'Articles', 'action'=>'index']);

Advanced options when the custom HTML helper is installed.

//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']);

Note: The www prefix should be used sparingly. If you require the www to be prepended to your domain, we would recommend installing a middleware plugin that forces the www to be added to the domain. In any case, setting 'prefix' = false is the best practice.

Contributing

Please help our project by creating a fork, and sending a pull request.

We need help writing unit tests, implementing Travis-CI and generally improving the functionality of the code.

To Do

  • Travis CI and PHPUnit Testing

License

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.
Comments
  • Prefix = False Behavior

    Prefix = False Behavior

    An issue arises when generation HTML links using the view helper. There are 4 possibilities when trying to generate a link:

    Prefix = Valid Subdomain

    This works as expected. When you have a valid subdomain that has been entered into the configuration, it should handle this type of link generation correctly. This is how this currently works:

    prefix.domain.com/controller/action

    Prefix = Invalid Subdomain

    When an invalid prefix is requested, (or a prefix that has not been routed) an error occurs. I do not know what happens when there is a valid route, but an invalid subdomain. Expected behavior would keep the host the same and append the prefix into the stream such as:

    www.domain.com/prefix/controller/action

    Prefix = False

    Currently this behavior does not work as I believe it should. Currently it does nothing to change the host. I believe this is wrong and it should instead remove any prefix that may be existing. It should purposefully strip any valid subdomain prefix off of the url and replace with either WWW or no-WWW. Here is an example:

    www.domain.com/controller/action

    Prefix = NULL

    When the Prefix is NULL (as should be the case most often). It should just keep whatever is already entered in for the prefix. That way you can create controllers that can be accessed and generated prefix independent. It will always use a prefix if there is already one, or www or no subdomain at all.

    Perhaps with all this WWW, there should be a spot in the configuration file to ask if WWW should be used or not. I personally have some sites that I prefer the WWW on and some that I do not.

    That being said, if we always OMIT the www, the end user can also install a middleware controller that forces WWW to be added.

    If anyone has any feedback on this, I'd like to hear it.

    bug enhancement 
    opened by ajquick 2
  • Plugin does not work on subdomain inputs

    Plugin does not work on subdomain inputs

    It appears that the plugin does not actually work to properly route requests into the proper prefixed subdomain controllers. This appears to be either a bug that is preventing this from working with plugins (it does work when used in the 'app' source). Or it could be that Traits and plugins do not work well together.

    I will investigate to see if using Traits isn't the best way to go about this. It is most likely that the trait will need to be adapted into a custom router, and then our routes will need to rely on the 'SubdomainRoute' as the default.

    Strangely, the code does work when used directly in the CakePHP src/app folder or namespace.

    opened by ajquick 2
  • hi it doesn't work

    hi it doesn't work

    Hello

    This is my code. //routes.php image

    I'have create a /config/subdomain.php ;like image

    //bootstrap.php

    Plugin::load('Multidimensional/Subdomains', ['bootstrap' => true, 'routes' => true]);
    

    When i visit www.domain.com and supplier.domain.com; they all match home page. What's wrong with me?

    help wanted 
    opened by slince 1
  • Non-Routed Subdomain Prepends URL

    Non-Routed Subdomain Prepends URL

    When your base CakePHP directory is located on a subdomain (default.example.com) that is not entered into the configuration, it will prepend the listed subdomain (admin) to the already subdomain'ed url. Like this:

    http://admin.default.example.com/
    

    Additionally, it does not remove the subdomain when making 'non' prefixed links.

    For example, when you are in admin.example.com, you cannot go back to a non subdomain prefix domain. Perhaps prefix needs to be set to null instead of false?

    opened by ajquick 1
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @ajquick requested this pull request.

    It consists of patches automatically generated for this project on Scrutinizer: https://scrutinizer-ci.com/g/multidimension-al/cakephp-subdomains/

    opened by scrutinizer-auto-fixer 0
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @ajquick requested this pull request.

    It consists of patches automatically generated for this project on Scrutinizer: https://scrutinizer-ci.com/g/multidimension-al/cakephp-subdomains/

    opened by scrutinizer-auto-fixer 0
  • Implement Custom Router

    Implement Custom Router

    This custom router no longer relies upon the 'Trait' method. A custom router will now help with routing the subdomains directly. If this becomes a problem, we may need to change it to extend 'DashedRoute' instead of regular Route. Further testing required.

    opened by ajquick 0
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @ajquick requested this pull request.

    It consists of patches automatically generated for this project on Scrutinizer: https://scrutinizer-ci.com/g/multidimension-al/cakephp-subdomains/

    opened by scrutinizer-auto-fixer 0
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @ajquick requested this pull request.

    It consists of patches automatically generated for this project on Scrutinizer: https://scrutinizer-ci.com/g/multidimension-al/cakephp-subdomains/

    opened by scrutinizer-auto-fixer 0
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @ajquick requested this pull request.

    It consists of patches automatically generated for this project on Scrutinizer: https://scrutinizer-ci.com/g/multidimension-al/cakephp-subdomains/

    opened by scrutinizer-auto-fixer 0
  • Scrutinizer Auto-Fixes

    Scrutinizer Auto-Fixes

    @ajquick requested this pull request.

    It consists of patches automatically generated for this project on Scrutinizer: https://scrutinizer-ci.com/g/multidimension-al/cakephp-subdomains/

    opened by scrutinizer-auto-fixer 0
Releases(v1.1.0)
  • v1.1.0(Jan 7, 2017)

    In this update we have fundamentally changed the way that the URLs are intercepted and matched by adding PSR7 middleware and a custom CakePHP router class. These new changes have been tested and are working in ways that previous releases were not. It is highly recommended that all previous versions be discontinued.

    Note: The configuration file schema has been changed. You must re-run the bin/cake SubdomainsInstall program or modify your 'subdomains.php' file to change the Multidimensional/Subdomains.subdomains variable to be capitalized in order to get your old configuration file to work.

    Source code(tar.gz)
    Source code(zip)
  • v1.0.1(Jan 3, 2017)

    We have fixed issue #7 that was causing subdomains to be stacked and prepended to domains that already had other subdomains. In addition, code quality was improved overall.

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0-dev(Jan 2, 2017)

  • v1.0.0(Jan 3, 2017)

    This is our first release of the code. This code has been tested thoroughly and is believed to be working well! If you find any bugs, please file an issue or create a fork and a pull request.

    We also need help implementing Travis-CI and phpunit tests. If you use this code, please help out by improving upon it.

    Source code(tar.gz)
    Source code(zip)
  • v0.0.1(Jan 1, 2017)

    This version should work on CakePHP versions 3.0 to 3.2.

    This branch will be unmaintained. All future releases will be aimed at CakePHP 3.3+.

    Source code(tar.gz)
    Source code(zip)
Owner
multidimension.al
multidimension.al
🔨 Prefixes all PHP namespaces in a file/directory to isolate the code bundled in PHARs.

PHP-Scoper PHP-Scoper is a tool which essentially moves any body of code, including all dependencies such as vendor directories, to a new and distinct

Humbug 590 Jan 2, 2023
Php-file-iterator - FilterIterator implementation that filters files based on a list of suffixes, prefixes, and other exclusion criteria.

php-file-iterator Installation You can add this library as a local, per-project dependency to your project using Composer: composer require phpunit/ph

Sebastian Bergmann 7.1k Jan 3, 2023
REDAXO 5 Addon für Metainfos an YRewrite-Domains auf Basis von YForm.

Meta-Infos für YRewrite Domains auf Basis von YForm 4 Ergänzt YRewrite um die Möglichkeit, Metainformationen an Domains zu verwalten. Mit vorgefertigt

alex+ Informationsdesign 9 Sep 19, 2022
Versi dinamis dari spk fuzzy sebelumnya, kini support setting jumlah sub kriteria

spk-fuzzy-dynamic Versi dinamis dari spk fuzzy sebelumnya, kini support setting jumlah sub kriteria SPK-Pariwisata-Fuzzy Implementasi Fuzzy Tahani Pad

Huda Mustakim 3 Mar 22, 2022
#️⃣ Generate, Save, and Route Stripe-like Hash IDs for Laravel Eloquent Models

Using this package you can generate, save and, route Stripe-like Hash Ids for your Eloquent Models. Hash Ids are short, unique, and non-sequential, an

Yunus Emre Deligöz 88 Dec 27, 2022
Easily add logs anywhere in your Magento 2 code

Magento 2 Simple Log Easily add logs anywhere in the code (like Magento 1). Requirements Requires Magento 2 in any version. Installation Add the Log.p

Matthieu Vion 19 Dec 27, 2021
Simple user settings facade for Hyperf. Settings are stored as JSON in a single database column, so you can easily add it to an existing table.

hyperf-user-settings Simple user settings util for hyperf Settings are stored as JSON in a single database column, so you can easily add it to an exis

lysice 1 Oct 15, 2021
This package makes it easy to add early access mode to your existing application.

This package makes it easy to add early access mode to your existing application. This is useful for when you want to launch a product and need to gat

Neo 174 Nov 26, 2022
The Workflow Package add Drag & Drop Workflows to your Laravel Application.

Workflows add Drag & Drop automation's to your Laravel application. The Workflow Package adds Drag & Drop Workflows to your Laravel Application. A Wor

42coders 196 Dec 29, 2022
Laravel Blog Package. Easiest way to add a blog to your Laravel website. A package which adds wordpress functionality to your website and is compatible with laravel 8.

Laravel Blog Have you worked with Wordpress? Developers call this package wordpress-like laravel blog. Give our package a Star to support us ⭐ ?? Inst

Binshops 279 Dec 28, 2022
Add scalar type hints and return types to existing PHP projects using PHPDoc annotations

PHPDoc to Type Hint Archived! This repository is now archived. Consider using PHP CS Fixer (and especially the phpdoc_to_param_type and phpdoc_to_retu

Kévin Dunglas 228 May 22, 2022
WooCommerce Empty Cart Button plugin helps you to add Empty Cart button to WooCommerce Pages/Sections using Shortcode only.

WooCommerce Empty Cart Button plugin helps you to add Empty Cart button to WooCommerce Pages/Sections using Shortcode only. How to use? Download the p

Katr 2 Sep 24, 2022
Testing your OpenApi documentation and your code easily.

Raven - How to test your API documentation and behavior. This library was written to allow testing OpenAPI documentation easily. It also allows verify

CH Studio 30 Dec 6, 2022
Fly50W is a new language which helps you build simple apps using more than 500k lines of code easily.

Fly50W is a new language which helps you build simple apps using more than 500k lines of code easily. Installation

null 4 Jun 22, 2022
This module adds a command to easily generate "modules" in Laravel and install them using composer.

Laravel Module Create This module adds a command to easily generate "modules" in Laravel and install them using composer Installation Simply install t

null 2 Feb 18, 2022
Re-skinning BotCloaker enables you to add a SMART button at the end of all posts on your blog.

Re-skinning BotCloaker Wordpress Plugin Re-skinning BotCloaker enables you to add a SMART button at the end of all posts on your blog. Re-skinning Bot

Mohammed cha 120 Nov 11, 2022
A Pocketmine-MP plugin to add King Of The Hill events to your server.

KOTH KOTH is an event popular on HCF and Faction servers. This plugin lets you add this minigame to you server. Support For questions, please join the

ItsMax123 9 Sep 17, 2022
Add a logger to your Kirby3 website

A Website logger for Kirby3 Commercial Usage This plugin is free but if you use it in a commercial project please consider to buy me a ?? Installation

Michael Scheurich 13 Nov 27, 2022
HelpBook - Add manuals to your server!

HelpBook - Add manuals to your server!

NovaStark1245 2 Jun 18, 2022