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.
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.
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 **/
);
}
}