Reputation: 427
Is there a way to disable artisan commands from running at all?
For example, if I wanted to disable php artisan migrate:fresh
from running, where would I go to remove/disable the command?
Upvotes: 6
Views: 5346
Reputation: 4684
As far as I know, laravel does not have this feature by default. And this is still under laravel ideas.
I also had this problem before and could not find a solution, I am not sure why you want to disable a command. But my case was that in the production
environment I never want to run php artisan migrate:fresh
. So what I end up with is to override the default command.
For example, in the routes/console.php
file:
if ('production' === App::environment()) {
Artisan::command('migrate:fresh', function () {
$this->comment('You are not allowed to do this in production!');
})->describe('Override default command in production.');
}
So, when you are in production
, php artisan migrate:fresh
will do nothing. You can change the condition based on your requirement, my example is just an idea of how you can override a laravel default command based on some variables in the .env
file.
You can do a lot of things here as well, I am not sure why you want to disable the command, so this is the best I can help.
Laravel v11.9.0 introduced a new Prohibitable
trait and added in a few destructive commands, migrate: fresh
is one of them.
All you need to do is add this to the boot method of your AppServiceProvider to prevent certain commands.
public function boot(): void
{
FreshCommand::preventFromRunning();
RefreshCommand::preventFromRunning();
ResetCommand::preventFromRunning();
WipeCommand::preventFromRunning();
}
Note: you may limit this to certain environments by passing a boolean argument to this method. For example, only preventing them in production.
FreshCommand::preventFromRunning($this->app->isProduction());
Or call all of them in one go
DB::prohibitDestructiveCommands($this->app->isProduction());
Here is the original PR
Upvotes: 18
Reputation: 2932
Create a command like the following
<?php
namespace App\Console\Commands\Utils;
use Illuminate\Console\Command;
use Illuminate\Console\Events\CommandStarting;
class PreCommand extends Command
{
protected $signature = 'precommand';
public function handle(CommandStarting $event) {
if (app()->environment(['production'])) {
logger('Trying to fresh database in production');
if ($event->command == 'migrate:fresh') {
$this->output = $event->output;
$this->info('You can not fresh database in the production');
die();
}
}
}
}
And register it in your EventServiceProvider's boot method
<?php
namespace App\Providers;
use App\Console\Commands\Utils\PreCommand;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
* Register any events for your application.
*
* @return void
*/
public function boot() {
Event::listen(CommandStarting::class, PreCommand::class);
}
}
Upvotes: 3