PHPackages                             swayok/laravel-site-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. swayok/laravel-site-loader

ActiveLibrary

swayok/laravel-site-loader
==========================

Classes to separate several different parts (sites) of a single project (i.e. admin and frontend)

01191PHP

Since Apr 16Pushed 7y ago1 watchersCompare

[ Source](https://github.com/swayok/laravel-site-loader)[ Packagist](https://packagist.org/packages/swayok/laravel-site-loader)[ RSS](/packages/swayok-laravel-site-loader/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (1)

What is this?
=============

[](#what-is-this)

For example: you have a project that contains several very different sites in it. Let's say - it is Frontend, Admin and SuperAdmin sites. All these sites have different resources (views, css, js, images, etc.) but use common DB models, classes, etc. Separating them will be a bad idea and might waste a lot of time.

So what if we use Service Providers?
------------------------------------

[](#so-what-if-we-use-service-providers)

This can be done when you have a simple situation. But if there are some register() and provides() methods you will need to manually detect if you need to load them or not, also you will need to do same verification for boot() method. Leaving this methods without conditions may be dangerous when you have `$this->app->singleton('\Class\Name', callback)` calls on same `'\Class\Name'` but with different `callback`. I've got many issues with it and finally made this classes to solve most of them as easy as possible.

How to use it:
--------------

[](#how-to-use-it)

### 1. Create site loaders

[](#1-create-site-loaders)

For each site in your project create a loader class that extends `\LaravelSiteLoader\AppSiteLoader`. (Or you can use `\LaravelSiteLoader\AppSiteLoaderInterface` to make you own version of `\LaravelSiteLoader\AppSiteLoader`

For frontend:

```
namespace App\Frontend;

use LaravelSiteLoader\AppSiteLoader;

class FrontendSiteLoader extends AppSiteLoader {

    static public function canBeUsed() {
        return (
            $_SERVER['REQUEST_URI'] === '/'
            || empty($_SERVER['REQUEST_URI'])
            || starts_with($_SERVER['REQUEST_URI'], static::getBaseUrl())
        );
    }

    static public function getBaseUrl() {
        return '/account';
    }

    public function boot() {
        static::setLocale();
        // your code here
    }

    static public function getDefaultLocale() {
        return 'en';
    }

    public function register() {
        // your registrations here
    }

    public function provides() {
        // your privides list here
        return [];
    }
}

```

For admin:

```
namespace PeskyCMF\CMS\CmsAdmin;

use LaravelSiteLoader\AppSiteLoader;

class AdminSiteLoader extends AppSiteLoader {

    static public function getBaseUrl() {
        return '/admin';
    }

    public function boot() {
        static::setLocale();
        // your code here
    }

    static public function getDefaultLocale() {
        return 'en';
    }

    public function register() {
        // your registrations here
    }

    public function provides() {
        // your privides list here
        return [];
    }
}

```

In `\LaravelSiteLoader\AppSiteLoader` there are some predefined fields:

```
/** @var AppSitesServiceProvider */
protected $provider;
/** @var Application */
protected $app;

```

and methods:

```
static public function canBeUsed() {
    return (
        $_SERVER['REQUEST_URI'] === '/'
        || empty($_SERVER['REQUEST_URI'])
        || starts_with($_SERVER['REQUEST_URI'], static::getBaseUrl())
    );
}

/**
 * @return ParameterBag
 */
protected function getAppConfig() {
    return config();
}

/**
 * Sets the locale if it exists in the session and also exists in the locales option
 *
 * @return void
 */
static public function setLocale() {
    $locale = session()->get(get_called_class() . '_locale');
    \App::setLocale($locale ?: static::getDefaultLocale());
}

/**
 * Configure session for current site
 * @param string $connection - connection name
 * @param int $lifetime - session lifetime in minutes
 */
public function configureSession($connection, $lifetime = 720) {
    $config = $this->getAppConfig()->get('session', ['table' => 'sessions', 'cookie' => 'session']);
    $this->getAppConfig()->set('session', array_merge($config, [
        'table' => $config['table'] . '_' . $connection,
        'cookie' => $config['cookie'] . '_' . $connection,
        'lifetime' => $lifetime,
        'connection' => $connection,
        'path' => static::getBaseUrl()
    ]));
}

```

Overwrite them if you need something specific.

### 2. Create and configure special service provider

[](#2-create-and-configure-special-service-provider)

Create a single service provider that extends `\LaravelSiteLoader\Providers\AppSitesServiceProvider` and contains some specific configs: `protected $defaultSectionLoaderClass` and `protected $sectionLoaderClasses`. Personally I use `AppServiceProvider` for this:

```
namespace App\Providers;

use LaravelSiteLoader\Providers\AppSitesServiceProvider;
use App\Frontend\FrontendSiteLoader;
use App\CmsAdmin\AdminSiteLoader;

class AppServiceProvider extends AppSitesServiceProvider {
    protected $defaultSectionLoaderClass = FrontendSiteLoader::class;

    protected $sectionLoaderClasses = [
        AdminSiteLoader::class,
    ];
}

```

Here:

- `protected $defaultSectionLoaderClass` is used whenever no loader from `protected $sectionLoaderClasses` can be used
- `protected $sectionLoaderClasses` list of all site loaders except default one

To detect correct loader `\LaravelSiteLoader\Providers\AppSitesServiceProvider` calls `AdminSiteLoader::canBeUsed()` method for all loader calsses listed in `$this->sectionLoaderClasses`. The first loader that returns true will be assigned to `$this->siteLoader` property. If there is no matching loaders - `$this->defaultSectionLoaderClass` loader will be assigned to `$this->siteLoader` property without `canBeUsed()` method call

### 3. Add your service provider to `config/app.php`

[](#3-add-your-service-provider-to-configappphp)

Notes
-----

[](#notes)

- In your ServiceProvider you can access matching loader via `$this->siteLoader`
- If you overwrite `boot()`, `register()` or `provides()` methods in your ServiceProvider - make sure you call `parent::boot()`, `parent::register()` and `parent::provides()` methods within your methods to save loaders functionality This code may be useful if you overload provides() method in service provider:

    public function provides() { return array\_unique(array\_merge( parent::provides(), \[ \\App\\Http\\Request::class, DbModel::class \] )); }
- In `\LaravelSiteLoader\Providers\AppSitesServiceProvider` I've rediclared some methods to be public:

    public function loadTranslationsFrom($path, $namespace) public function loadViewsFrom($path, $namespace) public function publishes(array $paths, $group = null)

So you can use them within loaders via `$this->provider->loadTranslationsFrom('/path', 'namespace')`

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a631e17770f1ef26d05cbc896e14b218d186973a00a515eed2d86fbc9eb936a?d=identicon)[sway](/maintainers/sway)

---

Top Contributors

[![swayok](https://avatars.githubusercontent.com/u/629675?v=4)](https://github.com/swayok "swayok (22 commits)")

### Embed Badge

![Health badge](/badges/swayok-laravel-site-loader/health.svg)

```
[![Health](https://phpackages.com/badges/swayok-laravel-site-loader/health.svg)](https://phpackages.com/packages/swayok-laravel-site-loader)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
