Instantly share code, notes, and snippets.

Embed
What would you like to do?
Lumen Key Generator Commands
<?php
namespace App\Console\Commands;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
class KeyGenerateCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'key:generate';
/**
* The console command description.
*
* @var string
*/
protected $description = "Set the application key";
/**
* Execute the console command.
*
* @return void
*/
public function fire()
{
$key = $this->getRandomKey();
if ($this->option('show')) {
return $this->line('<comment>'.$key.'</comment>');
}
$path = base_path('.env');
if (file_exists($path)) {
file_put_contents(
$path,
str_replace(env('APP_KEY'), $key, file_get_contents($path))
);
}
$this->info("Application key [$key] set successfully.");
}
/**
* Generate a random key for the application.
*
* @return string
*/
protected function getRandomKey()
{
return Str::random(32);
}
/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return array(
array('show', null, InputOption::VALUE_NONE, 'Simply display the key instead of modifying files.'),
);
}
}
@shamir92

This comment has been minimized.

shamir92 commented Dec 7, 2016

thanks a lot bro for your script

@vishal1221

This comment has been minimized.

vishal1221 commented Apr 20, 2017

Thanks Bro i am totaly new here where i can put this controller and how to run it?

@deargonaut

This comment has been minimized.

deargonaut commented May 16, 2017

Thanks!
Doesn't work on empty key. If you replace line 43 with this it does.

str_replace('APP_KEY=' . env('APP_KEY'), 'APP_KEY=' . $key, file_get_contents($path))
@a7madev

This comment has been minimized.

a7madev commented Jul 19, 2017

Thanks! I changed line 41-44 to
file_put_contents($path, str_replace('APP_KEY=' . env('APP_KEY'), 'APP_KEY=' . $key, file_get_contents($path)));

@iamandrewluca

This comment has been minimized.

iamandrewluca commented Jan 14, 2018

It seems that fire function changed to handle

flipboxstudio/lumen-generator@f07a9ae

@brenjt

This comment has been minimized.

brenjt commented Jul 5, 2018

Why not use Encrypter::generateKey()? Your solution could get you into trouble if a different cipher was used.

@haidi20

This comment has been minimized.

haidi20 commented Sep 14, 2018

thank you very much.. your script very help me.. thank you..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment