Reputation: 1218
I need to use the "Maintenance mode" using the artisan command php artisan down
, but just for some specific urls...
In my case, I want that all urls that starts with /admin
continue working.
Is there a solution?
Upvotes: 9
Views: 10157
Reputation: 1218
Suggest by @lukasgeiter
I created a middleware that tests my url...
That`s my code:
<?php namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\RedirectResponse;
class Maintenance {
public function handle($request, Closure $next){
if($request->is('admin*') || $request->is('maintenance')){
return $next($request);
}else{
return new RedirectResponse(url('/maintenance'));
}
}
}
After that I created a route
that show de maintenance view:
Route::get('maintenance', function(){
return view('errors.503');
});
Now I can call the command "up"
and the application still under maintenance, but the /admin
urls...
Upvotes: 1
Reputation: 328
In laravel 11.x they have introduced preventRequestsDuringMaintenance
so you can just do the following and laravel will take care of the rest:
// bootstrap/app.php
$middleware->preventRequestsDuringMaintenance(except: [
"v1/test/uri",
]);
Upvotes: 3
Reputation: 404
Take a look at app/http/middleware/CheckForMaintenanceMode.php
There is a URI Array:
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode as Middleware;
class CheckForMaintenanceMode extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array
*/
protected $except = [
//
];
}
array-example :
protected $except = [
'api/customers',
'api/user'
];
Upvotes: 23
Reputation: 1208
I did something like this before, but not using the php artisan down
command.
The first thing the kernel does before even loading the application is checking if your site is in maintenance mode.
So, you won't be able to reach your controller or even your routes with php artisan down
.
You might want to do your own command likephp artisan softdown
which create a file somewhere in your /storage dir which will tell to your app the app is in 'soft down' mode. And of course, make another one called softup
Then, in your routes, you'll wrap all your frontend routes with an IF which checks if the file exists, or not.
If so, then you return the view maintenance.blade.php.
Upvotes: 0
Reputation: 351
I'm also using this to check if any of the URL segments is API, to let those URL's to work properly.
public function handle($request, Closure $next) {
if ($this->app->isDownForMaintenance() && !in_array($_SERVER['REMOTE_ADDR'],['192.168.240.18','127x.0.0.1'])) {
/** if URL contains API, disable continue, don't keep maintenance */
if(in_array('api',$request->segments())){
return $next($request);
}
throw new HttpException(503);
}
return $next($request);
}
Upvotes: 0