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

Feature: Automatically add global middleware using config #4

Open
bilfeldt opened this issue Sep 29, 2023 · 0 comments
Open

Feature: Automatically add global middleware using config #4

bilfeldt opened this issue Sep 29, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@bilfeldt
Copy link
Owner

bilfeldt commented Sep 29, 2023

Instead of manually having to add the middlewares globally, then we could add a config (default disabled) to automatically push these middlewares to the global stack.

This is done in itsgoingd/clockwork

protected function registerMiddleware(): void
{
        // The interface does not have the methods hasMiddleware() and prependMiddleware() but the actual implementation does
	$kernel = $this->app[\Illuminate\Contracts\Http\Kernel::class];

	if (method_exists($kernel, 'hasMiddleware') && $kernel->hasMiddleware(ClockworkMiddleware::class): {
	    return;
        }

        if (! method_exists($kernel, 'prependMiddleware')) {
	    return;
        }

	$kernel->prependMiddleware(ClockworkMiddleware::class);
}

And also in Laravel Jetstream even without the checks.

protected function bootInertia()
{
    $kernel = $this->app->make(Kernel::class);

    $kernel->appendMiddlewareToGroup('web', ShareInertiaData::class);
    $kernel->appendToMiddlewarePriority(ShareInertiaData::class);

    if (class_exists(HandleInertiaRequests::class)) {
        $kernel->appendToMiddlewarePriority(HandleInertiaRequests::class);
    }

// ...
}

Question: Is there no better way? It seems incorrect to "assume" (or check for existence) of methods not defined on the interface.

@bilfeldt bilfeldt added the enhancement New feature or request label Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant