Trong bài viết này tôi sẽ nói về các lệnh cơ bản trong Magento 2. Như các bạn đã biết Magento 2 sử dụng giao diện dòng lệnh để thực hiện một số các hành động cụ thể. Điều này khá là khó khăn đối với các bạn mới tiếp cận với Magento 2. Nên trong bài viết này tôi sẽ hướng dẫn một vài lệnh cơ bản hay sử dụng trong quá trình làm việc với Magento 2.
1. Xem danh sách các lệnh cơ bản trong Magento 2.
1 2 3 |
php bin/magento bin/magento php bin/magento list |
Khi bạn chạy một trong 3 lệnh trên bạn sẽ nhận được danh sách các lệnh đang có sẵn trong Magento 2. Bao gồm cả các lệnh tùy chỉnh mà bạn đã thêm vào. Bạn có thể xem cách để thêm một lệnh tùy chỉnh vào Magento 2 tại bài viết này. Sau đó bạn sẽ nhận được danh sách các lệnh như thế này.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
Magento CLI 2.4.1 Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: help Displays help for a command list Lists commands admin admin:user:create Creates an administrator admin:user:unlock Unlock Admin Account app app:config:dump Create dump of application app:config:import Import data from shared configuration files to appropriate data storage app:config:status Checks if config propagation requires update braintree braintree:migrate Migrate stored cards from a Magento 1 database cache cache:clean Cleans cache type(s) cache:disable Disables cache type(s) cache:enable Enables cache type(s) cache:flush Flushes cache storage used by cache type(s) cache:status Checks cache status catalog catalog:images:resize Creates resized product images catalog:product:attributes:cleanup Removes unused product attributes. command command:vimagento:example ViMagento console example config config:sensitive:set Set sensitive configuration values config:set Change system configuration config:show Shows configuration value for given path. If path is not specified, all saved values will be shown cron cron:install Generates and installs crontab for current user cron:remove Removes tasks from crontab cron:run Runs jobs by schedule customer customer:hash:upgrade Upgrade customer's hash according to the latest algorithm deploy deploy:mode:set Set application mode. deploy:mode:show Displays current application mode. dev dev:di:info Provides information on Dependency Injection configuration for the Command. dev:profiler:disable Disable the profiler. dev:profiler:enable Enable the profiler. dev:query-log:disable Disable DB query logging dev:query-log:enable Enable DB query logging dev:source-theme:deploy Collects and publishes source files for theme. dev:template-hints:disable Disable frontend template hints. A cache flush might be required. dev:template-hints:enable Enable frontend template hints. A cache flush might be required. dev:template-hints:status Show frontend template hints status. dev:tests:run Runs tests dev:urn-catalog:generate Generates the catalog of URNs to *.xsd mappings for the IDE to highlight xml. dev:xml:convert Converts XML file using XSL style sheets dotdigital dotdigital:connector:automap Auto-map data fields dotdigital:connector:enable Add Dotdigital API credentials and enable the connector dotdigital:migrate Migrate data into email_ tables to sync with Engagement Cloud dotdigital:sync Run syncs to populate email_ tables before importing to Engagement Cloud downloadable downloadable:domains:add Add domains to the downloadable domains whitelist downloadable:domains:remove Remove domains from the downloadable domains whitelist downloadable:domains:show Display downloadable domains whitelist encryption encryption:payment-data:update Re-encrypts encrypted credit card data with latest encryption cipher. i18n i18n:collect-phrases Discovers phrases in the codebase i18n:pack Saves language package i18n:uninstall Uninstalls language packages indexer indexer:info Shows allowed Indexers indexer:reindex Reindexes Data indexer:reset Resets indexer status to invalid indexer:set-dimensions-mode Set Indexer Dimensions Mode indexer:set-mode Sets index mode type indexer:show-dimensions-mode Shows Indexer Dimension Mode indexer:show-mode Shows Index Mode indexer:status Shows status of Indexer info info:adminuri Displays the Magento Admin URI info:backups:list Prints list of available backup files info:currency:list Displays the list of available currencies info:dependencies:show-framework Shows number of dependencies on Magento framework info:dependencies:show-modules Shows number of dependencies between modules info:dependencies:show-modules-circular Shows number of circular dependencies between modules info:language:list Displays the list of available language locales info:timezone:list Displays the list of available timezones inventory inventory:reservation:create-compensations Create reservations by provided compensation arguments inventory:reservation:list-inconsistencies Show all orders and products with salable quantity inconsistencies inventory-geonames inventory-geonames:import Download and import geo names for source selection algorithm maintenance maintenance:allow-ips Sets maintenance mode exempt IPs maintenance:disable Disables maintenance mode maintenance:enable Enables maintenance mode maintenance:status Displays maintenance mode status media-content media-content:sync Synchronize content with assets media-gallery media-gallery:sync Synchronize media storage and media assets in the database module module:config:status Checks the modules configuration in the 'app/etc/config.php' file and reports if they are up to date or not module:disable Disables specified modules module:enable Enables specified modules module:status Displays status of modules module:uninstall Uninstalls modules installed by composer newrelic newrelic:create:deploy-marker Check the deploy queue for entries and create an appropriate deploy marker. queue queue:consumers:list List of MessageQueue consumers queue:consumers:start Start MessageQueue consumer sampledata sampledata:deploy Deploy sample data modules for composer-based Magento installations sampledata:remove Remove all sample data packages from composer.json sampledata:reset Reset all sample data modules for re-installation security security:recaptcha:disable-for-user-forgot-password Disable reCAPTCHA for admin user forgot password form security:recaptcha:disable-for-user-login Disable reCAPTCHA for admin user login form setup setup:backup Takes backup of Magento Application code base, media and database setup:config:set Creates or modifies the deployment configuration setup:db-data:upgrade Installs and upgrades data in the DB setup:db-declaration:generate-patch Generate patch and put it in specific folder. setup:db-declaration:generate-whitelist Generate whitelist of tables and columns that are allowed to be edited by declaration installer setup:db-schema:add-slave Move checkout quote related tables to a separate DB server setup:db-schema:split-quote Move checkout quote related tables to a separate DB server setup:db-schema:split-sales Move sales related tables to a separate DB server setup:db-schema:upgrade Installs and upgrades the DB schema setup:db:status Checks if DB schema or data requires upgrade setup:di:compile Generates DI configuration and all missing classes that can be auto-generated setup:install Installs the Magento application setup:performance:generate-fixtures Generates fixtures setup:rollback Rolls back Magento Application codebase, media and database setup:static-content:deploy Deploys static view files setup:store-config:set Installs the store configuration. Deprecated since 2.2.0. Use config:set instead setup:uninstall Uninstalls the Magento application setup:upgrade Upgrades the Magento application, DB data, and schema store store:list Displays the list of stores store:website:list Displays the list of websites support support:backup:code Create Code backup support:backup:db Create DB backup support:utility:check Check required backup utilities support:utility:paths Create utilities paths list theme theme:uninstall Uninstalls theme varnish varnish:vcl:generate Generates Varnish VCL and echos it to the command line yotpo yotpo:reset Reset Yotpo sync flags &/or configurations yotpo:sync Sync Yotpo manually (reviews module) yotpo:update-metadata Manually send platform metadata to Yotpo |
2. Danh mục
Chúng ta có thể chia danh sách các lệnh trên thành các loại sau:
- Cache
- Indexer
- Cron job
- Code compiler
- Set the Magento mode
- i18n translation
- Run unit tests
- Maintenance
- module management
- Setup: Install, Upgrade, Uninstall, Backup , Rollback
- Theme
- Deployment
Tiếp theo, tôi sẽ lấy một vài ví dụ về từng loại.
3. Quản lý cache
Cách kiểm tra trạng thái của cache:
1 |
<code>php bin/magento cache:status</code> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Current status: config: 1 layout: 1 block_html: 1 collections: 1 reflection: 1 db_ddl: 1 compiled_config: 1 eav: 1 customer_notification: 1 config_integration: 1 config_integration_api: 1 full_page: 1 target_rule: 1 config_webservice: 1 translate: 1 vertex: 1 |
Làm mới (dọn dẹp) bộ nhớ cache
1 2 |
<code>php bin/magento cache:clean</code>hoặc php bin/magento c:c |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
➜ vi php bin/magento c:c Cleaned cache types: config layout block_html collections reflection db_ddl compiled_config eav customer_notification config_integration config_integration_api full_page target_rule config_webservice translate vertex |
Xóa bộ nhớ cache:
1 2 |
<code>php bin/magento cache:flush</code>hoặc php bin/magento c:f |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
➜ vi php bin/magento c:f Flushed cache types: config layout block_html collections reflection db_ddl compiled_config eav customer_notification config_integration config_integration_api full_page target_rule config_webservice translate vertex |
Bật hoặc tắt cache
1 2 |
<code>php bin/magento cache:enable php bin/magento cache:disable</code> |
Nâng cao hơn bạn có thể bật, tắt một vài cache type mà bạn muốn
1 2 |
php bin/magento cache:enable [cache_type] ... [cache_type] php bin/magento cache:disable<span style="background-color: inherit; color: rgb(57, 58, 52); font-family: monospace; font-size: inherit;"> [cache_type] ... [cache_type]</span> |
Ví dụ về tắt các cache type: eav và full_page
1 |
php bin/magento cache:disable eav full_page |
4. Quản lý Indexer
Xem danh sách các indexer
1 |
php bin/magento indexer:info |
Xem trạng thái indexer
1 |
php bin/magento indexer:status |
Reindex data
1 |
php bin/magento indexer:reindex |
Chuyển trạng thái của index thành invalid. (chuyển thành invalid để cron reindex lại)
1 |
php bin/magento indexer:reset |
Kiểm tra indexer đang ở chế độ nào (Update on save hay Update on schedule)
1 |
php bin/magento indexer:show-mode |
Đặt chế độ cho index
1 |
<code>php bin/magento indexer:set-mode {realtime|schedule} [indexer]</code> |
Ví dụ
1 |
<code>magento indexer:set-mode schedule catalog_category_product catalog_product_category</code> |
5. Deploy static content
Cú pháp:
1 |
<code>php bin/magento setup:static-content:deploy <lang> ... <lang> [--dry-run]</code> |
Cách deploy static content
1 |
php bin/magento setup:static-content:deploy |
1 |
<code>$ php bin/magento setup:static-content:deploy Requested languages: en_US === frontend -> Magento/blank -> en_US ===</code> |
Mặc định nó chỉ deploy cho ngôn ngữ en_EN
Nếu bạn muốn deploy cho ngôn ngữ khác như pt_BR, bạn cần chạy lệnh như sau
1 |
<code>php bin/magento setup:static-content:deploy pt_BR</code> |
Kết quả:
1 2 3 |
➜ vi php bin/magento setup:static-content:deploy pt_BR Deploy using quick strategy frontend/Magento/blank/pt_BR 2587/2684 ==========================>- 96% 7 secs |
6. Quản lý maintenance mode
Bật maintenance mode
1 |
<code>php bin/magento maintenance:enable</code> |
Tắt maintenance mode
1 |
<code>php bin/magento maintenance:disable</code> |
7. Developer hoặc Production mode
Kiểm tra mode hiện tại
1 |
<code>php bin/magento deploy:mode:show</code> |
Chuyển sang developer mode
1 |
<code>php bin/magento deploy:mode:set developer</code> |
Chuyển sang production mode
1 |
php bin/magento deploy:mode:set production |
8. Generate các file factory, interceptor, proxy…
1 |
php bin/magento setup:di:compile |
Bài viết liên quan
Tham khảo thêm tại đây: https://devdocs.magento.com/guides/v2.4/reference/cli/magento.html