Drush
Drush is a program for managing a Drupal site from the terminal. Everything it does can also be done in the panel, but a command is faster: rebuild the cache, enable a module, log in as the administrator. A developer runs it dozens of times a day.
Running it
Drush was installed into the project in the previous lesson and lives in vendor/bin. It is run from the project folder:
vendor/bin/drush statusThe command prints the Drupal version, the PHP version, the database name and the site path. If you see an error instead, you are not in the project folder.
From here on the course writes just drush for short — it means vendor/bin/drush.
Rebuilding the cache
vendor/bin/drush cache:rebuildThe short form is drush cr. Drupal remembers the list of routes, hooks, services, templates and plugins so that it does not have to look for them on every request. That is why, after you change module files, nothing new shows up until the cache is rebuilt. You need this command after every lesson of the course.
Main commands
| Command | Short | What it does |
|---|---|---|
drush cache:rebuild | drush cr | Rebuilds the cache |
drush pm:install blog | drush en blog | Enables a module |
drush pm:uninstall blog | drush pmu blog | Uninstalls a module and removes its data |
drush pm:list | drush pml | The list of modules and their state |
drush user:login | drush uli | A one-time link to log in as the administrator |
drush watchdog:show | drush ws | The latest log entries |
drush cron | Runs the scheduled tasks | |
drush status | drush st | Information about the site |
drush help shows help for any command, for example drush help pm:install.
Installing a ready-made module
Ready-made modules from drupal.org are downloaded by Composer and enabled by Drush. Let us install Admin Toolbar — it adds drop-down menus to the panel:
composer require drupal/admin_toolbar
vendor/bin/drush en admin_toolbarThe first command puts the module into web/modules/contrib/admin_toolbar, the second enables it. After a page reload the items of the top panel menu open on hover.
Logging in without a password
vendor/bin/drush uliThe command prints an address like http://default/user/reset/1/.... Open it in the browser, replacing default with 127.0.0.1:8888, and you are logged in as the administrator.
Common mistake
You added a route, a hook or a template, and nothing changed on the site. The cause is almost always the cache: run drush cr. Make it a rule to run this command after any change to .yml files, after adding a hook and after adding a new plugin class.