Created
January 20, 2019 10:27
-
-
Save artoodetoo/cbb9dcbf268aa9082cd3cc4e88907418 to your computer and use it in GitHub Desktop.
Laravel: run standalone console script without touching the project code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sometimes there is a need to execute one-time set of commands without changing the project code. | |
For the simplest cases, Laravel has amazing `artisan tinker` command. But what if you want a little more than doing a single line of code, or you want to apply it on several servers. | |
Here for these short scripts, I wrote this snippet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* The code mostly peeped in artisan executable file. | |
* Put this file to project root (one level above DocumentRoot) and run | |
* $ php example.php | |
*/ | |
if (php_sapi_name() !== 'cli') { | |
die('This file can be executed from console only'); | |
} | |
require_once __DIR__.'/vendor/autoload.php'; | |
/* @var $app \Illuminate\Foundation\Application */ | |
/* @var $kernel \App\Http\Kernel */ | |
$app = require_once __DIR__.'/bootstrap/app.php'; | |
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class); | |
$status = 0; // success | |
$input = new Symfony\Component\Console\Input\ArgvInput; | |
$output = new Symfony\Component\Console\Output\ConsoleOutput; | |
$kernel->bootstrap(); | |
$num = DB::update("UPDATE `users` SET `nickname` = 'artoodetoo' WHERE `nickname`='r2d2'"); | |
$output->writeln("{$num} nickname(s) updated in `users`"); | |
// ... | |
// ... other usefull payload ... | |
// ... | |
$kernel->terminate($input, $status); | |
exit($status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just used the knowledge gained here to create a one off script to setup the admin account. Intent is to delete the file in production after using it only once.
Thank you.