0

As I am not sure how to describe it, I didn't found any results in google or stack.

I would like to list all available console commands (which are callable by using the bin/console) with a Controller-Action so that I can forward a list of all commands to twig.

How can I realize this ?

4
  • Maybe this could help you stackoverflow.com/questions/64458501/… ? Commented Jul 18, 2022 at 14:15
  • @DylanKas not relevant to the question because he wants to display them in a twig template! Commented Jul 19, 2022 at 9:25
  • So ? Using this in a twig extension would help rendering it in a twig template Commented Jul 19, 2022 at 9:29
  • I need the Console commands for adding a user function which makes console commands (cronjobs) callable manually whenever. So, I don not really display them, but I need the value of it, thats why I said I use them in twig (controller, twig, all the same :)) Commented Jul 19, 2022 at 10:28

1 Answer 1

2

Interesting question. You can of course just run the console command itself and capture the list of commands. Might actually be the best way.

However, there is a service called console.command_loader which has a method called getNames which does indeed return a list of command names. It implements CommandLoaderInterface.

Originally I tried to create an alias so it could be injected into an action method:

services:
   Symfony\Component\Console\CommandLoader\CommandLoaderInterface: 
       alias: console.command_loader

But I kept getting console.command_loader not found which was puzzling since debug:container shows it. The service was tagged with container.no_preload which might have something to do with it. Not sure.

So I went and just defined the controller service:

services:
    App\Controller\CommandController:
        tags:
            - 'controller.service_arguments'
        arguments:
            - '@console.command_loader'

And somewhat to my surprise it worked.

class CommandController extends AbstractController
{
    public function __construct(private CommandLoaderInterface $cl)
    {
    }
   
    #[Route('/commands', name: 'app_commands')]
    public function commands(): Response
    {
        $names = $this->cl->getNames();
        dump($names);

        // I happen to have a command called app:init
        $initCommand = $this->cl->get('app:init');
        dump($initCommand->getDescription());

        //return $this->render('default/index.html.twig', [
        //    'controller_name' => 'DefaultController ' . 'Commands',
        //]);
    }
}

This was all done in Symfony 6. Did not happen to have a Symfony 3 app handy. Your first step would be to confirm that Symfony 3 also has the service with bin/console debug:container console.command_loader. If it does not have such a service then poke around a bit and see if it has something similar.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Cerad for your explanation and your snippets. This is it!
Thank you for the thank you but stackoverflow tends to frown upon thank you type comments. On the other hand, accepting an answer makes it clear that the answer did the trick and strongly implies a thank you..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.