- We can set any configuration options from the command line.
- We use the commands given below to set the Magento 2 configuration:
- Set configuration values
- Set configuration values that cannot be edited in the Magento Admin
- Display the value of configuration settings
- Show all saved configuration
- Show all saved configuration for the base website
- Show Sender Email of Sales Representative configuration for default scope
- Show Sender Email for Sales Representative configuration for the base website
- FAQs
In this blog post, I will guide you on how to change the Magento 2 configuration from the CLI command. So without wasting much time, let’s dive into the post.
We can set any configuration options from the command line.
- Before installing Magento, one can set the configuration values for the default scope only, which is the only valid scope.
- After installing Magento, You can set the configuration values for any website or store view scope which is quite beneficial.
We use the commands given below to set the Magento 2 configuration:
- bin/magento config:set sets any non-sensitive configuration value by its configuration path.
- bin/magento config:sensitive:set sets any sensitive configuration value by its configuration path.
- bin/magento config:show shows saved configuration values; values of encrypted settings that are displayed as asterisks.
To set a configuration value, you must know at least one of the following:
- The configuration path
- The scope code, to set a configuration value for a particular scope
Set configuration values
- To set system-specific configuration values on Magento 2.2.0 – 2.2.3, use:
$ bin/magento config:set [--scope="..."] [--scope-code="..."] [-l | --lock] path value
- To set system-specific configuration values on Magento 2.2.4 and higher, use:
$ bin/magento config:set [--scope="..."] [--scope-code="..."] [-le | --lock-env] [-lc | --lock-config] path value
- To set sensitive configuration values, use:
$ bin/magento config:sensitive:set [--scope="..."] [--scope-code="..."] path value
- The
config:sensitive:set
command writes sensitive settings to<Magento base directory>/app/etc/env.php
.
- The
–scope : Defines scope of the configuration. Valid values are default, website, or store. The default value is set as default.
–scope-code: Defines scope code of configuration (website code or store view code).
-l or –lock (Version: 2.2.0 – 2.2.3) : Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/env.php
file.
-le or –lock-eav (Version 2.2.4 and higher) : Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/env.php
file.
-lc or –lock-config(Version 2.2.4 and higher) : Either locks the value so it cannot be edited in the Magento Admin or changes a setting that is already locked in the Magento Admin. The command writes the value to the <Magento base dir>/app/etc/config.php
file. The –lock-config option overwrites –lock-env if you specify both the options.
Path : Required. The configuration path.
Value : Required. The value of the configuration.
Example:
- Set Sender Email for Sales Representative with default scope:
$ bin/magento config:set trans_email/ident_sales/email sales@aureatelabs.com
- Set the Sender Email for Sales Representative for the base website:
$ bin/magento config:set --scope="website" --scope-code="base" trans_email/ident_sales/email aureate_sales@aureatelabs.com
- Set the Sender Email for Sales Representative for the test store view:
$ bin/magento config:set --scope="stores" --scope-code="default" trans_email/ident_sales/email aureate_sales@aureatelabs.com
Set configuration values that cannot be edited in the Magento Admin
If you have used –lock-eav (version 2.2.4 or higher) or –lock (version 2.2.0-2.2.3) option to set Magento configuration, this command will save the configuration value in <Magento base dir>/app/etc/eav.php
file and will disable the config field for further editing the value in the admin.
$ bin/magento config:set --lock-env --scope=stores --scope-code=default trans_email/ident_sales/email sales@aureatelabs.com
If you have used –lock-config (version 2.2.4 or higher) option to set Magento configuration, this command saves configuration value in <Magento base dir>/app/etc/config.php
file and disables the config field for edit value in the admin.
$ bin/magento config:set --lock-config --scope=stores --scope-code=default web/url/use_store 1
Display the value of configuration settings
$ bin/magento config:show [--scope[="..."]] [--scope-code[="..."]] path
Example:$ bin/magento config:show --scope="stores" --scope-code="default" web/url/use_store
Result :1
Show all saved configuration
$ bin/magento config:show
Result :web/seo/use_rewrites - 1
web/unsecure/base_url - http://127.0.0.1/magento/
web/unsecure/base_static_url -
web/unsecure/base_media_url -
web/secure/base_url - https://127.0.0.1/magento/
web/secure/use_in_frontend - 0
web/secure/use_in_adminhtml - 0
web/secure/base_static_url -
web/secure/base_media_url -
.
.
.
Show all saved configuration for the base website
$ bin/magento config:show --scope="website" --scope-code="base"
Result :web/unsecure/base_url - http://127.0.0.1/magento/
trans_email/ident_sales/email - aureate_sales@aureatelabs.com
.
.
Show Sender Email of Sales Representative configuration for default scope
$ bin/magento config:show trans_email/ident_sales/email
Result :sales@aureatelabs.com
Show Sender Email for Sales Representative configuration for the base website
$ bin/magento config:show --scope="website" --scope-code="base" trans_email/ident_sales/email
If the configuration value is not set for the website, then it will give an error as shown below:Configuration for path: "trans_email/ident_sales/email" doesn't exist
Hope this detailed article served your purpose. Feel free to leave the comments below and contact us if you need any help to customize your Magento store.
Thanks for your interest.
FAQs
The CLI command in Magento 2 stands for Command Line Interface. The interface allows you to perform a variety of tasks including commerce installation, updates, clearing cache memory, and other such configuration tasks utilizing commands. You can also add, modify, or list all available commands through the command line interface.
To access Magento from command line, follow the below steps:
1. Open the command prompt on the Magento server.
2. Redirect the directory to the folder where Magento is installed.
3. Run the command “chmod 744 bin/magento” to execute the CLI.
4. Once the CLI is executable, utilize bin/magento to use the CLI and also list all the commands.
To make a CLI command in Magento 2, follow these steps –
1. Create a new module in the app/code directory of your Magento installation.
2. In the module’s etc directory, create a new module.xml file.
3. Add the following code to register the module with Magento:<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Vendor_Module" setup_version="1.0.0"/>
</config>
4. Now, in the module’s Console directory, create a new Command class, and it must implement the configure() & execute() methods.
<?php
namespace Vendor\Module\Console;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CommandName extends Command
{
protected function configure()
{
$this->setName('vendor:commandname')
->setDescription('Command Description');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
//Add your command logic here
$output->writeln('Command executed successfully.');
}
}
5. Further, create a new di.xml file in the module’s etc directory by adding the code –?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="vendor_module_commandname"
xsi:type="object">Vendor\Module\Console\CommandName</item>
</argument>
</arguments>
</type>
</config>
6. Run the command php bin/magento setup:upgrade in your Magento root directory to enable the module.
7. Thereafter, run the command php bin/magento vendor:commandname to execute the command.
The correct way to execute the Magento CLI command is by using php bin/magento <command> in the root directory. For example, to run the command cache:clean, use php bin/magento cache:clean. You can also use the list command php bin/magento list to retrieve all the available other commands.
Post a Comment
Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️