Create custom command Concrete CMS

User
Create custom command in concrete cms 9 and run via terminal
Assistant
To create a **custom command in Concrete CMS 9** and run it via terminal, follow these steps:

### ✅ 1. **Create Package Structure (if not already)**
Your custom command should live inside a package. Example package path:

packages/my_custom_package/

### ✅ 2. **Create the Command Class**
Inside your package, create a new directory:
`packages/my_custom_package/src/Command`

Create a file `HelloWorldCommand.php`:

<?php
namespace ConcretePackageMyCustomPackageCommand;

use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;

class HelloWorldCommand extends Command
{
    protected static $defaultName = 'my-custom:hello';

    protected function configure()
    {
        $this
            ->setDescription('Outputs Hello World message.')
            ->setHelp('This command lets you print a hello world message');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $output->writeln('<info>Hello from Concrete CMS custom command!</info>');
        return Command::SUCCESS;
    }
}

### ✅ 3. **Register Command in Package Controller**

Open or create:
`packages/my_custom_package/controller.php`

Inside `on_start()` method, register the command:

use ConcreteCoreConsoleConsoleRunner;
use ConcretePackageMyCustomPackageCommandHelloWorldCommand;

public function on_start()
{
    $this->app->extend(ConsoleRunner::class, function ($runner) {
        $runner->add(new HelloWorldCommand());
        return $runner;
    });
}

Make sure you have the correct namespace at the top:

namespace ConcretePackageMyCustomPackage;

use ConcreteCorePackagePackage;

### ✅ 4. **Install or Reinstall Your Package**

If it’s a new package, install via the dashboard or CLI:

./concrete/bin/concrete c5:package-install my_custom_package

If it’s already installed and you modified code, do:

./concrete/bin/concrete c5:package-uninstall my_custom_package
./concrete/bin/concrete c5:package-install my_custom_package

### ✅ 5. **Run Your Custom Command via Terminal**

./concrete/bin/concrete my-custom:hello

You should see:

Hello from Concrete CMS custom command!

Let me know if your package is not being detected or if the command is not registering — I’ll help debug.

Leave a Comment

Your email address will not be published. Required fields are marked *