appstract / laravel-multisite by gizburdt

Multisite setup for Laravel
10,255
214
18
Package Data
Maintainer Username: gizburdt
Maintainer Contact: hello@appstract.team (Gijs Jorissen)
Package Create Date: 2017-02-24
Package Last Update: 2019-05-23
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2024-05-04 15:11:22
Package Statistics
Total Downloads: 10,255
Monthly Downloads: 19
Daily Downloads: 0
Total Stars: 214
Total Watchers: 18
Total Forks: 31
Total Open Issues: 1

Laravel Multisite

Latest Version on Packagist Total Downloads Software License Build Status

With this package it is possible to build multiple sites/(sub)domains on one codebase.

Installation

You can install the package via composer:

composer require appstract/laravel-multisite

Config (hosts, homestead)

You need to add the sites to your /etc/hosts file and Homestead.yaml. For example, mywebsite.dev and blog.mywebsite.dev. In the Homestead.yaml, you need to map the sites to the same folder.

Publish

By running php artisan vendor:publish --provider="Appstract\Multisite\MultisiteServiceProvider" in your project all files for multisite will be published. The files that will be published are: a migration, a seeder and a config file.

Seeder

The seeder will be published but needs to be run when running php artisan db:seed, so you need the add $this->call(SitesTableSeeder::class); to your DatabaseSeeder.php file. After migrating and seeding the sites are now present in the database.

Usage

This is the main part, within your routes/web.php you can set routes for your sites within route groups, like this:

Route::group([
    'domain' => 'blog.'.config('multisite.host'),
    'as' => 'blog.',
    'middleware' => 'site:blog'
], function () {
    Route::get('/', 'BlogController@homepage')->name('homepage');
});

The magic happens with the site middleware site:blog. This will tell your app that the routes within the group are belonging to the blog. It will provide a variable called $currentSite in all your views. There is also a config available, which you can access with Config::get('multisite.site').

Testing

$ composer test

Contributing

Contributions are welcome, thanks to y'all :)

About Appstract

Appstract is a small team from The Netherlands. We create (open source) tools for webdevelopment and write about related subjects on Medium. You can follow us on Twitter, buy us a beer or support us on Patreon.

License

The MIT License (MIT). Please see License File for more information.