GithubHelp home page GithubHelp logo

Comments (2)

devhereco avatar devhereco commented on June 2, 2024 1

Filament PHP Admin Panel - Asset Path Challenge in Tenancy Applications

Dear Developers, community,

I hope this message finds you well. Following our discussion on the challenges associated with asset paths in tenancy applications when utilizing Filament PHP as an admin panel, I'm pleased to present a comprehensive solution that I've implemented.

The Issue:

The primary obstacle arises from the dynamic nature of asset paths in tenancy applications. Specifically, when dealing with a tenancy application, the asset path can vary between being a subdomain and an external domain linked to the application. Currently, there are limited references addressing this challenge, prompting me to share my observations and seek community input for potential solutions.

The Solution:

In the AppServiceProvider.php, within the boot method, I've incorporated the following code to dynamically handle asset paths:

 // Retrieve the domain and subdomain from the cache if available
list($subdomain, $domain) = Cache::remember('current_subdomain_domain', now()->addDay(), function () {
    // Parse the URL to extract the host (domain)
    $parsedUrl = parse_url(URL::current());

    // Get the host (domain) from the parsed URL
    $host = $parsedUrl['host'];

    // Split the host into subdomain and domain
    $segments = explode('.', $host, 2);
    $subdomain = isset($segments[0]) ? $segments[0] : null;
    $domain = isset($segments[1]) ? $segments[1] : null;

    return [$subdomain, $domain];
});
        
// Cache domain-specific assets path
$assetsPath = Cache::remember("assets_path_$subdomain.$domain", now()->addDay(), function () use ($subdomain, $domain) {
    return "https://$subdomain.$domain";
});

config(['filament.assets_path' => $assetsPath]);
\Log::info("Current Subdomain: $subdomain, Current Domain: $domain");

// Check if the current URL is not the same as app.central_domain
if ("$subdomain.$domain" !== config('app.central_url')) {
    \Log::info("Condition matched. Adding middleware.");

    // Only run the code if the current URL is not the same as central_domain and is a subdomain
    Livewire::setUpdateRoute(function ($handle) use ($subdomain, $domain) {
        return Route::post('/livewire/update', $handle)
            ->middleware([
                'web',
                InitializeTenancyByDomainOrSubdomain::class
            ]);
    });

    Artisan::call('route:clear');
}

Feel free to adapt and improve this solution based on your specific application requirements. I encourage the community to provide feedback, suggestions, or enhancements to further refine this approach.

Best regards,
Mohammed B,

from tenancy.

sheenazien8 avatar sheenazien8 commented on June 2, 2024

Hey @devhereco, I'm looking for how to implement filament using this tenancy, but I found nothing,

I've got the weird error that the table from the tenant database is not found because the database is still using the central database

I already implemented the InitializeTenancyByDomain::class in Livewire::setUpdateRoute method, and then I already initialized the tenant using the subdomain, but I still get that error

        $panel = $panel
            ->id('tenant')
            ->colors([
                'primary' => Color::Amber,
            ])
            ->path('/member')
            ->login()
            ->discoverResources(in: app_path('Filament/Tenant/Resources'), for: 'App\\Filament\\Tenant\\Resources')
            ->discoverPages(in: app_path('Filament/Tenant/Pages'), for: 'App\\Filament\\Tenant\\Pages')
            ->pages([
                Pages\Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Tenant/Widgets'), for: 'App\\Filament\\Tenant\\Widgets')
            ->widgets([
                Widgets\AccountWidget::class,
                Widgets\FilamentInfoWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ]);

        $url = request()->getHost();
        $domain = explode('.', $url);
        if (count($domain) > 2) {
            tenancy()->initialize($domain[0]);
            $tenant = tenancy()->tenant;
            $subdomain = $tenant?->domains?->first()?->domain;
            $panel
                ->domain($subdomain);
        }

        return $panel;

I'm pleased if you can help solve my issue Thanks;

from tenancy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.