As well as the title stated ‘Laravel Generate Key via Command’, this article is actually used for generating key for web-application based on Laravel framework. This article relates to the CSRF token generated and maintained by Laravel framework to be used for further authentication every time an HTML form exist in the page is submitted.
A hidden input field named ‘csrf_token’ will be used for sending an unique value to be validated and authenticated in order to protect the form from CSRF (Cross Site Request Forgery) attack.
So, the preparation is done by executing the following steps :
1. Make sure that the ‘cipher’ in the file named app.php located in folder config inside the root folder of web-application based on Laravel framework has already set as follows :
'cipher' => 'AES-256-CBC',
or
'cipher' => 'AES-256-CBC',
2. Generate the key defined in .env file which is located in the root folder of web-application based on Laravel framework. This is the part which is directly associated with the title of this article. So, in order to generate the key, just type the following command in the bash prompt :
php artisan key:generate
Below is the example of the command execution in a real situation :
user@hostname:/var/www/html/testing/laravel-project$ php artisan key:generate Application key [base64:8bNcWn9rjqhmSQcY6wmU2h0dr6SZx6vHCoYo6KNByqQ=] set successfully. user@hostname:/var/www/html/testing/laravel-project$
3. Soon after the process for generating key has successfully carried out, check the value of APP_KEY field in the file named .env located in the root folder of web-application based on Laravel framework :
user@hostname:/var/www/html/testing/laravel-project$ vim .env
There should be the following content regarding the value of APP_KEY which must be similar with the one generated via command executed above as shown below :
APP_KEY=base64:8bNcWn9rjqhmSQcY6wmU2h0dr6SZx6vHCoYo6KNByqQ=
So, the above is one of the content of file .env whenever the process for generating key has been successfully carried out. If the file is corrupt or whenever the line of geneted key is erased or missing. It can be regenerated or recovered by re-executing the above command.
One thought on “Laravel Generate Key via Command”