Protect or disable laravel artisan commands in production

While working on a product at VentureCraft, I wanted to make sure that there was no way I could accidentally screw myself over by running something like migrate:refresh in production. I put up a teaser on twitter which got some good attention so I thought I’d share the nasty code I used to achieve this.

The tweet:

The code:

At this stage, all I’m doing is adding a snippet of code to the top of my app/artisan.php file, which performs the check. At a later stage I planned on moving this to a package so I can use it in other projects, however I received the following tweet from Taylor which hopefully will make that redundant and he’ll just build it into the core (a touch more elegantly I’m sure):

What’s more, in the package I’d planned on allowing to pick and choose which commands are protected in which environment based on a config file, the following hopefully will take care of that too:

Update

Stoked to see that this has indeed been added into the core, as a trait you can add to your own commands

artisanlaravel

April 05, 2014

© 2020 ChrisDuell.com