HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/hi.naniguide.com/app/Helpers/workspace_url.php
<?php

if (!function_exists('wsRoute')) {
    /**
     * Generate a workspace-aware URL for a named route.
     *
     * When a workspace is active, customer.* routes are rewritten to
     * /customer/w/{slug}/... so the URL reflects the workspace context.
     *
     * @param  string  $name       Named route (e.g. 'customer.dashboard')
     * @param  mixed   $parameters Route parameters
     * @param  bool    $absolute   Whether to generate an absolute URL
     * @return string
     */
    function wsRoute(string $name, mixed $parameters = [], bool $absolute = true): string
    {
        $url = route($name, $parameters, $absolute);

        // Only rewrite customer.* routes
        if (!str_starts_with($name, 'customer.')) {
            return $url;
        }

        $workspace = view()->shared('activeWorkspace');

        if (!$workspace || !$workspace->slug) {
            return $url;
        }

        // Insert /w/{slug} after /customer in the URL
        $slug = $workspace->slug;
        $prefix = $absolute ? url('customer') : '/customer';

        if (str_contains($url, $prefix)) {
            $url = preg_replace(
                '#(' . preg_quote($prefix, '#') . ')(/|$)#',
                '$1/w/' . $slug . '$2',
                $url,
                1
            );
        }

        return $url;
    }
}