15.如何使用控制台命令

Magento 2 开发目录

本文研究一下Magento系统提供的一些命令。Magento 通过 bin/magento 提供了很多命令,刚开始感觉会有些复杂,接下来我们就仔细了解一下:

当运行如下命令

php bin/magento

bin/magento

就会输出所有可用的命令列表,当括我们自定义的命令:

Usage:                                                                                                                      
 command [options] [arguments]                                                                                              
                                                                                                                            
Options:                                                                                                                    
 --help (-h)           Display this help message                                                                            
 --quiet (-q)          Do not output any message                                                                            
 --verbose (-v|vv|vvv) Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug   
 --version (-V)        Display this application version                                                                     
 --ansi                Force ANSI output                                                                                    
 --no-ansi             Disable ANSI output                                                                                  
 --no-interaction (-n) Do not ask any interactive question                                                                  
                                                                                                                            
Available commands:                                                                                                         
 help                                      Displays help for a command                                                      
 list                                      Lists commands                                                                   
admin                                                                                                                       
 admin:user:create                         Creates an administrator                                                         
 admin:user:unlock                         Unlock Admin Account                                                             
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.                                               
cron                                                                                                                        
 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:source-theme:deploy                   Collects and publishes source files for theme.                                   
 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                                         
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-mode                          Sets index mode type                                                             
 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                                         
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                                                 
module                                                                                                                      
 module:disable                            Disables specified modules                                                       
 module:enable                             Enables specified modules                                                        
 module:status                             Displays status of modules                                                       
 module:uninstall                          Uninstalls modules installed by composer                                         
sampledata                                                                                                                  
 sampledata:deploy                         Deploy sample data modules                                                       
 sampledata:remove                         Remove all sample data packages from composer.json                               
 sampledata:reset                          Reset all sample data modules for re-installation                                
setup                                                                                                                       
 setup:backup                              Takes backup of Magento Application code base, media and database                
 setup:config:set                          Creates or modifies the deployment configuration                                 
 setup:cron:run                            Runs cron job scheduled for setup application                                    
 setup:db-data:upgrade                     Installs and upgrades data in the DB                                             
 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                                                 
 setup:uninstall                           Uninstalls the Magento application                                               
 setup:upgrade                             Upgrades the Magento application, DB data, and schema                            
theme                                                                                                                       
 theme:uninstall                           Uninstalls theme      

命令行分类

可以将上面的列表分为以下类别:

  • 缓存 Cache
  • 索引器 Indexer
  • 定时任务 Cron Job
  • 代码编译器 Code Compiler
  • 设置系统模式 Set the magento mode
  • i18n 国际化翻译 i18n translation
  • 运行单元测试 unit test
  • 维护模式 Maintenance
  • 模块管理 Module management
  • 安装 Setup: Install, upgrde, uninstall, backup, rollback
  • 主题 theme
  • 部署 deployment

接下来看下每个类别的一些示例:

缓存命令

查看缓存状态

php bin/magento cache:status

输出:

                    config: 1
                    layout: 1
                block_html: 1
               collections: 1
                    db_ddl: 1
                       eav: 1
                 full_page: 1
                 translate: 1
        config_integration: 1
    config_integration_api: 1
         config_webservice: 1

清除缓存:

php bin/magento cache:clean

输出:

$ php bin/magento cache:clean                  
Cleaned cache types:                           
config                                         
layout                                         
block_html                                     
collections                                    
reflection                                     
db_ddl                                         
eav                                            
customer_notification                          
full_page                                      
config_integration                             
config_integration_api                         
translate                                      
config_webservice  

如何刷新缓存:

php bin/magento cache:flush

如何启用或停用缓存:

php bin/magento cache:enable
php bin/magento cache:disable

# 加参数
magento cache:enable [type] ... [type]
magento cache:disable [type] ... [type]

# 示例 停用页面缓存
magento cache:disable full_page

索引器命令

查看索引状态

php bin/magento indexer:info

输出:

design_config_grid                       Design Config Grid
customer_grid                            Customer Grid
catalog_category_product                 Category Products
catalog_product_category                 Product Categories
catalog_product_price                    Product Price
catalog_product_attribute                Product EAV
catalogsearch_fulltext                   Catalog Search
cataloginventory_stock                   Stock
catalogrule_rule                         Catalog Rule Product
catalogrule_product                      Catalog Product Rule

重建索引:

php bin/magento indexer:reindex

重置索引

php bin/magento indexer:reset

显示索引模式

php bin/magento indexer:show-mode

输出:

$ php bin/magento indexer:show-mode

Design Config Grid:                                Update on Save
Customer Grid:                                     Update on Save
Category Products:                                 Update on Save
Product Categories:                                Update on Save
Product Price:                                     Update on Save
Product EAV:                                       Update on Save
Catalog Search:                                    Update on Save
Stock:                                             Update on Save
Catalog Rule Product:                              Update on Save
Catalog Product Rule:                              Update on Save

设置索引模式类型

php bin/magento indexer:set-mode {realtime|schedule} [indexer]

# 示例:
magento indexer:set-mode scedule catalog_category_product catalog_product_category

部署命令

语法:

php bin/magento setup:static-content:deploy <lang> ... <lang> [--dry-run]

发布静态内容

php bin/magento setup:static-content:deploy

输出:

$ php bin/magento setup:static-content:deploy

Requested languages: en_US
=== frontend -> Magento/blank -> en_US ===
...........................................

发布静态内容默认使用语言: en_EN

如果要使用其它语言如简体中文: zh_Hans_CN

运行

php bin/magento setup:static-content:deploy zh_Hans_CN

使用命令获取后台URI路径

可以不用查看 etc/env.php 文件也能知道后台路径:

php bin/magento info:adminuri

使用命令管理维护模式

# 开启维护模式
php bin/magento maintenance:enable

# 关闭
php bin/magento maintenance:disable

设置允许指定IP访问维护模式的网站

php bin/magento maintenance:allow-ips

# 示例:
php bin/magento maintenance:allow-ips 168.168.168.168

设置系统开发者模式或线上模式

有3种模式定义在 Magneto\Framework\App\State: default, developer, production

显示当前系统模式

php bin/magento deploy:mode:show

# 默认输出 default

切换到开发者模式:

php bin/magento deploy:mode:set developer

恭喜你老铁!你的店铺现在就是开发者模式了。开始享受写代码的快感吧 :)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容