Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Livewire integration with path/request data identification #247

Open
lukinovec opened this issue Jun 1, 2023 · 0 comments
Open

[4.x] Livewire integration with path/request data identification #247

lukinovec opened this issue Jun 1, 2023 · 0 comments

Comments

@lukinovec
Copy link
Collaborator

lukinovec commented Jun 1, 2023

To use kernel identification, add InitializeTenancyByPath or InitializeTenancyByRequestData to $middleware in your App/Http/Kernel.php. To use route-level identification, apply the middleware directly to the route – with route-level path identification, you also need to re-register the routes.

Livewire
- v2 – In the livewire.php config file (if you don't have it, publish it using php artisan livewire:publish --config), change the value of 'middleware' to ['universal', 'web'].
- v3 – update the Livewire update route so that it's universal

Use UrlBindingBootstrapper in your tenancy.php config. The bootstrapper swaps the current 'url' (UrlGenerator) instance for our TenancyUrlGenerator.

a) Path Identification

Set TenancyUrlGenerator's $prefixRouteNames property to true.

// E.g. in TenancyServiceProvider's boot()
TenancyUrlGenerator::$prefixRouteNames = true;

Make sure you re-register routes (including the LW routes) in your TenancyServiceProvider's boot() method.

Livewire v2
Assign ['universal', 'web'] (or ['universal', 'web', IdentificationMW::class] if you want route-level identification) to the'middleware_group'LW config key (in livewire.php).

Call the following code in your TenancyServiceProvider's boot() method:

Note: Don't forget to also re-register the routes when using route-level path identification with package routes

if (InitializeTenancyByPath::inGlobalStack()) {
    /** @var ReregisterUniversalRoutes $reregisterRoutesAction */
    $reregisterRoutesAction = app(ReregisterRoutesAsTenant::class);
    
    $reregisterRoutesAction->reregisterUsing('livewire.message-localized', function (Route $route) {
        $route->setUri(str($route->uri())->replaceFirst('locale', $tenantParameter = PathTenantResolver::tenantParameterName()));
        $route->parameterNames[0] = $tenantParameter;
        $route->middleware('tenant');
    });

    $reregisterRoutesAction->handle();
}

Also, update the app URL in your layout's script like this:

<script>
    let tenantKey = @json(tenant()?->getTenantKey())

    if(tenantKey) {
        window.livewire_app_url = '/' + tenantKey
    }
</script>

Livewire v3
Call the following code in your TenancyServiceProvider's boot() method:

Livewire::setUpdateRoute(function ($handle) {
     return Route::post('/livewire/update', $handle)->middleware(['web', 'universal']);
});

if (InitializeTenancyByPath::inGlobalStack()) {
    TenancyUrlGenerator::$prefixRouteNames = true;
            
    /** @var ReregisterUniversalRoutes $reregisterRoutes */
    $reregisterRoutes = app(ReregisterUniversalRoutes::class);
    
    $reregisterRoutes->handle();
}

b) Request data identification

Make sure the TenancyUrlGenerator's $prefixRouteNames property is false.

// E.g. in TenancyServiceProvider's boot()
TenancyUrlGenerator::$prefixRouteNames = false;

Livewire v2
Add the tenant header to the global window.Livewire.connection.headers JS property in your layout view, e.g.:

<script>
    let tenantKey = @json(tenant()?->getTenantKey())

    if(tenantKey) {
        window.Livewire.connection.headers = {
            ...window.Livewire.connection.headers,
            @json(\Stancl\Tenancy\Middleware\InitializeTenancyByRequestData::$header): tenantKey
        }
    }
</script>

Livewire v3
Call the following code in your TenancyServiceProvider's boot() method:

Livewire::setUpdateRoute(function ($handle) {
    return Route::post('/livewire/update', $handle)->middleware(['web', 'universal']);
});

Add the tenant header in your layout view like this:

@if(tenant())
    <script>
        let _fetch = window.fetch;

        window.fetch = (resource, options = {}) => {
            if (options.headers && options.headers['X-Livewire'] !== undefined) {
   options.headers[@json(\Stancl\Tenancy\Middleware\InitializeTenancyByRequestData::$header)] = @json(tenant()->getTenantKey());
            }

            return _fetch(resource, options);
        };
    </script>
@endif

To make file uploads work, follow the real-time facades docs page to create framework directories.

@lukinovec lukinovec changed the title [4.x] Livewire integration with path or request data identification [4.x] Livewire integration with path/request data identification Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant