Skip to content

Feature: Automatically add global middleware using config #4

@bilfeldt

Description

@bilfeldt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions