Add custom commands

cmuench edited this page Nov 30, 2014 · 2 revisions

You can assign your own symfony2 commands to n98-magerun.

  1. Add the folder which contains your custom commands to the autoloader.
  2. Register Commands

The config file must be placed in your home directory with the name ~/.n98-magerun.yaml.

It's also possible (since version 1.36.0) to place a config in your magento installation to add custom project based command. Create config and save it as app/etc/n98-magerun.yaml. The project config can use the variable %root% to get magento root folder dynamically.

Since version 1.72.0 it's possible to stucture commands in modules. See Modules.


  # Namespace => path to your libs
  MyCommandNamespace: /home/myuser/lib

  # or in project based config i.e.:
  # MyCommandNamespace: %root%/lib

    - MyCommandNamespace\FooCommand

  # optional command config
    bar: zoz

Example Command


namespace MyCommandNamespace;

use N98\Magento\Command\AbstractMagentoCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FooCommand extends AbstractMagentoCommand
    protected function configure()
          ->setDescription('Foo test command')

    * @param \Symfony\Component\Console\Input\InputInterface $input
    * @param \Symfony\Component\Console\Output\OutputInterface $output
    * @return int|void
    protected function execute(InputInterface $input, OutputInterface $output)
      if ($this->initMagento()) {
         $output->writeln(__DIR__ . ' -> ' . __CLASS__);


Run an existing command in a command


$input = new StringInput('cache:flush');

// ensure that n98-magerun doesn't stop after first command

// without output
$this->getApplication()->run($input, new NullOutput());

// with output
$this->getApplication()->run($input, $output);

// reactivate auto-exit

Get Magento Root-Folder


$rootFolder = $this->getApplication()->getMagentoRootFolder();

Enterprise Edition Only Commands

Add isEnable method to your command.


 * @return bool
public function isEnabled()
    return $this->getApplication()->isMagentoEnterprise();