Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Smiffy's avatar

Allowing access to url when in maintenance mode via a package

I have a package which defines, in its config, a url. This url should be available even when the site is in maintenance mode. I know that you can add to the $except array of App\Http\Middleware\PreventRequestsDuringMaintenance but it would be great if my package can automatically add the url into the $except array rather than ask the user of my package to remember to do this.

I can create some middleware which extends from Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance and define the exclusion with:

public function getExcludedPaths()
{
    return [
        config('my-package.my-excluded-url')
    ];
}

The question is then how could this be used to override App\Http\Middleware\PreventRequestsDuringMaintenance (if this is even possible/a good idea - I suspect not in both cases). I could ask the user to replace the entry in App\Http\Kernel.php but this is kind of the same problem. Is there some Laravel magic that can be employed? Any help or guidance appreciated.

0 likes
3 replies
illuminatixs's avatar

Hey there,

As far as I know, but correct me if I am wrong, you can't really replace this or overwrite this behavior without editing the App\Http\Kernel.php file. I would suggest to just add whatever method you end up using to the “installation guide” of your package's readme.md file.

Many other packages use an installation guide within their readme.md file.

Smiffy's avatar

Sorry this a late post. I managed to sort this out in the end.

My approach was to define the route within my package with middleware:

Route::post('/post-something',  MyController::class)->middleware('restrict_access');

Then in my ServiceProvider, I was able to assign any middleware I wanted using the following:

$router = $this->app->make(Illuminate\Routing\Router::class);
$router->pushMiddlewareToGroup('restrict_access', MyMiddleware::class);

MyMiddleWare extended Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance so I could use getExcludedPaths

Amaury's avatar

For everyone who wants to allow access to certain urls, there is a nice and simple Laravel way:

In your AppServiceProvider just add in the boot method:


<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;

class AppServiceProvider extends ServiceProvider
{
    // ...
    
    public function boot(): void
    {
        // ...
        
        // Indicate that the given URIs should always be accessible.
        CheckForMaintenanceMode::except(
            uris: '/uri/accessible*' /** array|string  $uris **/
        );
    }
}

Please or to participate in this conversation.