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

Add new TenantNotFoundForRequestEvent #502

Merged

Conversation

jacobmllr95
Copy link
Contributor

This PR adds a new TenantNotFoundForRequestEvent which get's fired when no tenant was found by the findForRequest() method of the TenantFinder.

While it is possible to write your own TenantFinder and implement the behavior yourself, I think this is a good addition to have out of the box for people who don't want to go this extra step.

The event itself can be useful for scenarios where a tenant could not be found for the current subdomain and you wan't to redirect to e.g. the root domain.

@masterix21
Copy link
Collaborator

Would you describe a real example case?

@jacobmllr95
Copy link
Contributor Author

@masterix21 Assumed you have an application where each tenant has it's own subdomain and the Laravel application handles the traffic for all wildcard subdomains except some known subdomains like www.yourtenancyapp.com.

This feature would come in handy when e.g. a request for unknown.yourtenancyapp.com comes in and there is no matching tenant for that namespace and you would like to redirect to the root domain in that case.

The event listener in that case could be something like:

<?php

namespace App\Listeners;

use Illuminate\Support\Facades\Redirect;
use Spatie\Multitenancy\Events\TenantNotFoundForRequestEvent;

class TenantNotFoundForRequestEventListener
{
    /**
     * Handle the event.
     *
     * @param TenantNotFoundForRequestEvent $event
     * @return void
     */
    public function handle(TenantNotFoundForRequestEvent $event): void
    {
        // When no tenant could be resolved by request, redirect to the root domain.
        Redirect::away('www.yourtenancyapp.com')->send();
        exit();
    }
}

@masterix21 masterix21 merged commit 2d9550f into spatie:main Jan 9, 2024
16 checks passed
@masterix21
Copy link
Collaborator

Thanks!

@jacobmllr95 jacobmllr95 deleted the feat-tenant-not-found-for-request-event branch January 9, 2024 12:21
@newtonjob
Copy link

@jacobmllr95 Although this is already merged, I wouldn't recommend sending a redirect response from an event listener and exiting the request flow right there, as it doesn't allow the response to go out normally through the Laravel request pipeline.

It makes more sense to do this kind of stuff in a middleware.

    public function handle($request, Closure $next)
    {
        if (! Tenant::checkCurrent()) {
            return redirect()->away('www.yourtenancyapp.com');
        }

        return $next($request);
    }

Alternatively, the NeedsTenant middleware that comes out of the box will throw a NoCurrentTenant exception and you can handle this in your App\Exceptions\Handler to return a redirect response.

   /**
     * Register the exception handling callbacks for the application.
     */
    public function register(): void
    {
        $this->renderable(function (NoCurrentTenant $e) {
            return redirect()->away('www.yourtenancyapp.com');
        });
    }

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

Successfully merging this pull request may close these issues.

3 participants