Grav command

Grav comes with a built-in command-line interface (CLI) which can be found at bin/grav. The CLI is extremely useful for running recurring tasks such as clearing the cache, making backups, and more.

Accessing the CLI is a simple process but you need to use a terminal. On a mac this is called Terminal, on windows, it's called cmd and on Linux, it's just a shell. UNIX style commands are not natively available in Windows cmd. Installing the msysgit package on a Windows machine adds Git and Git BASH, which is an alternative command prompt that makes UNIX commands available. If you are accessing your server remotely, you most likely will use SSH to remotely log in to your server. Check out this great tutorial for more information on SSH.

Although some operations can be performed manually, by relying on the CLI, these tasks could be automated via cronjobs that run daily.

To get a list of all the commands available in Grav, you can run the command:

bin/grav list

This should display something like:

Available commands:
  backup       Creates a backup of the Grav instance
  clean        Handles cleaning chores for Grav distribution
  clear-cache  [clearcache] Clears Grav cache
  composer     Updates the composer vendor dependencies needed by Grav.
  help         Displays help for a command
  install      Installs the dependencies needed by Grav. Optionally can create symbolic links
  list         Lists commands
  new-project  [newproject] Creates a new Grav project with all the dependencies installed
  sandbox      Setup of a base Grav system in your webroot, good for development, playing around or starting fresh
  security     Capable of running various Security checks

To get help for a specific command, you can prepend help to the command:

bin/grav help install

Backup

Backing up your project is nothing more than creating an archive of the ROOT of Grav. No Database, no complications.

Of course, you can simplify this even more by just using the Grav CLI. Supposing we have our ~/workspace/portfolio project and that we want to create a backup of it, here's what we will do:

cd ~/workspace/portfolio
bin/grav backup

A new backup portfolio-20140812174352.zip file has been created at the backup/ folder of the project. The long number after the name is just the date in the format of year month day hour minute second.

Clean

This CLI command is primarily used during the package building process, as it removes extraneous files and folders from Grav. It is strongly recommended you do not use this yourself unless you are using it build your own Grav packages.

bin/grav clean

Clear-Cache

You can clear the cache by deleting all the files and folders under cache/.

The equivalent CLI command is:

$ cd ~/webroot/my-grav-project
bin/grav clear-cache

There are several aliases for compatibility (clear-cache, clearcache, clear).

The default option is the standard cache clearing process however, you can control this further with these options:

--all             If set will remove all including compiled, twig, doctrine caches
--assets-only     If set will remove only assets/*
--images-only     If set will remove only images/*
--cache-only      If set will remove only cache/*
--tmp-only        If set will remove only tmp/*

Composer

If you installed Grav via GitHub and have manually installed composer-based vendor packages, you can easily update with:

bin/grav composer

You can also pass composer options such as install:

bin/grav composer --install

or

bin/grav composer --update

These all use the --no-dev composer option, so to be able to perform testing you should use composer directly: bin/composer.phar

Install

To install the dependencies Grav relies on (error plugin, problems plugin, antimatter theme), launch a terminal or console and navigate to the grav folder where you want to install the dependencies and run the CLI command.

$ cd ~/webroot/my-grav-project
bin/grav install

You should now have the dependencies installed under:

  • ~/webroot/my-grav-project/user/plugins/error
  • ~/webroot/my-grav-project/user/plugins/problems
  • ~/webroot/my-grav-project/user/themes/antimatter

And verbose output with stack traces:

bin/grav logviewer -v                                                                                                       [16:12:12]

Log Viewer
==========

viewing last 20 entries in grav.log

2019-03-14 05:52:44 [WARNING] Plugin 'simplesearch.bak' enabled but not found! Try clearing cache with `bin/grav clear-cache`
2019-03-14 05:52:44 [CRITICAL] A function must be an instance of \Twig_FunctionInterface or \Twig_SimpleFunction.
0 /Users/joe/my-grav-project/plugins/acme-twig-filters/acme-twig-filters.php(52): Twig\Environment->addFunction(Object(Twig\TwigFilter))
1 /Users/joe/my-grav-project/vendor/symfony/event-dispatcher/EventDispatcher.php(212): Grav\Plugin\ACMETwigFiltersPlugin->onTwigInitialized(Object(RocketTheme\Toolbox\Event\Event), 'onTwigInitializ...', Object(RocketTheme\Toolbox\Event\EventDispatcher))
2 /Users/joe/my-grav-project/vendor/symfony/event-dispatcher/EventDispatcher.php(44): Symfony\Component\EventDispatcher\EventDispatcher->doDispatch(Array, 'onTwigInitializ...', Object(RocketTheme\Toolbox\Event\Event))
3 /Users/joe/my-grav-project/vendor/rockettheme/toolbox/Event/src/EventDispatcher.php(23): Symfony\Component\EventDispatcher\EventDispatcher->dispatch('onTwigInitializ...', Object(RocketTheme\Toolbox\Event\Event))
4 /Users/joe/my-grav-project/system/src/Grav/Common/Grav.php(365): RocketTheme\Toolbox\Event\EventDispatcher->dispatch('onTwigInitializ...', Object(RocketTheme\Toolbox\Event\Event))
5 /Users/joe/my-grav-project/system/src/Grav/Common/Twig/Twig.php(175): Grav\Common\Grav->fireEvent('onTwigInitializ...')
6 /Users/joe/my-grav-project/system/src/Grav/Common/Processors/TwigProcessor.php(24): Grav\Common\Twig\Twig->init()
7 /Users/joe/my-grav-project/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php(45): Grav\Common\Processors\TwigProcessor->process(Object(Nyholm\Psr7\ServerRequest), Object(Grav\Framework\RequestHandler\RequestHandler))
8 /Users/joe/my-grav-project/system/src/Grav/Framework/RequestHandler/Traits/RequestHandlerTrait.php(57): Grav\Framework\RequestHandler\RequestHandler->handle(Object(Nyholm\Psr7\ServerRequest))
9 /Users/joe/my-grav-project/system/src/Grav/Common/Processors/AssetsProcessor.php(28): Grav\Framework\RequestHandler\RequestHandler->handle(Object(Nyholm\Psr7\ServerRequest))

2019-03-14 05:52:46 [WARNING] Plugin 'simplesearch.bak' enabled but not found! Try clearing cache with `bin/grav clear-cache`
...

New Project

Every time you want to start a new project with Grav, you need to start with a clean Grav instance. Through the CLI, this process is super easy and takes only a few seconds.

  1. Launch a terminal or console and navigate to the grav folder (for the sake of this document we will assume it resides under ~/Projects/grav)
cd ~/Projects/grav
  1. Run the Grav CLI to create a new project, with the destination being the location where your project will reside in (usually the webroot of your Web server). Let us assume we are creating a portfolio and we want it at ~/Webroot/portfolio.
bin/grav new-project ~/webroot/portfolio

This will create a new Grav instance and download all the dependencies required.

Sandbox

Grav has a nifty utility called sandbox, which can quickly create a symlinked copy of the Grav-installation. Simply put, running bin/grav sandbox -s DESTINATION - where "DESTINATION" is the path to the folder where you want the copied installation - recreates the Grav-installation in another folder.

For example, running:

bin/grav sandbox -s ../copy

From your current Grav-folder creates a sibling-folder named copy, where the following folders are virtual copies: /bin, /system, /vendor, /webserver-configs, as well as standard files that typically reside in Grav's root-folder. All content in /user will be carbon copies, not virtual, so you can easily get started with customizing the new installation without having created overhead from core files.

Security

Added in Grav 1.5 is a new security scanner CLI command. You can run this to quickly scan your contents against the configured security settings.

bin/grav security                                                                                       [12:34:12]

Grav Security Check
===================

Scanning 11 pages [===================================================] 100% < 1 sec

[OK] Security Scan complete: No issues found...

PHP CGI-FCGI Information

To determine if your server is running cgi-fcgi on the command line, type the following:

$ php -v
PHP 5.5.17 (cgi-fcgi) (built: Sep 19 2014 09:49:55)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with the ionCube PHP Loader v4.6.1, Copyright (c) 2002-2014, by ionCube Ltd.

If you see a reference to (cgi-fcgi) you will need to prefix all bin/grav commands with php-cli. Alternatively, you can set up an alias in your shell with something like: alias php="php-cli" which will ensure the CLI version of PHP runs from the command line.

Found errors? Think you can improve this documentation? Simply click the Edit link at the top of the page, and then the icon on Github to make your changes.

Results