May 29th, 2024

Protect your Laravel app in production with the new `Prohibitable` Trait

Protect your Laravel app in production with the new `Prohibitable` Trait

In a recent update to Laravel, a significant enhancement has been introduced that aims to safeguard production environments from potentially destructive commands: the Prohibitable trait, a feature designed to prevent inadvertent execution of commands that could disrupt operational systems.

Jason McCreary, the creator of Laravel Shift (and many more), contributed a new trait that will prevent disaster and aims to safeguard production environments from potentially destructive commands.

What is the Prohibitable Trait?

The Prohibitable trait is a new addition to Laravel that allows you to mark certain artisan commands as "destructive" and prevent their execution in production environments. This trait is particularly useful for commands like migrate:fresh, refresh, reset, and wipe, which, while useful during development, can lead to significant data loss if run accidentally in a production setting.

How Does It Work?

The implementation of the Prohibitable trait is straightforward yet powerful. You can add this trait to any artisan command and then specify in their AppServiceProvider or a similar service provider which commands should be prevented from running. Here’s a quick example:

1public function boot(): void
2{
3 DB::prohibitDestructiveCommands($this->app->isProduction());
4}

This will prohibit all commands that have the Prohibitable trait and the following commands by default: Fresh, Refresh, Reset and Wipe

By default, these commands will terminate immediately with a warning and a non-zero exit code if an attempt is made to execute them, effectively adding a safety layer that prevents accidental data loss.

Environment-Specific Safety

One of the key features of the Prohibitable trait is its flexibility to be enabled conditionally based on the environment. For instance, you might want to allow these commands in a local or staging environment but prevent them in production. This can be easily managed by passing a boolean condition to the prohibitDestructiveCommands method:

1prohibitDestructiveCommands($this->app->isProduction());

Community Reception and Future Prospects

The introduction of the Prohibitable trait has been met with positive feedback. The discussion on the pull request suggests potential future enhancements, such as extending this functionality to more commands or integrating it more deeply with Laravel's core functionalities.

Let's see it in action

Statamic Ninja

Comments

Marian Pop

PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.

Subscribe to our newsletter

Get latest news, tutorials, community articles and podcast episodes delivered to your inbox.

Weekly articles
We send a new issue of the newsletter every week on Friday.
No spam
We'll never share your email address and you can opt out at any time.