Symfony2 Console: Getting Started with Console Helpers

Originally published at: http://www.sitepoint.com/symfony2-console-getting-started-console-helpers/

Lately, I’ve been spending a significant amount of time working with the Symfony 2 Console Component in order to process different tasks, especially cronjobs. In this tutorial, I’ll share my experiences and we’ll give some extra love to the console helpers, which provide us with a large collection of handy functions. There are a lot of reasons to create console commands in your projects: sending emails, exporting/importing data, creating users, and so on.

Digital information

Introduction

First, I recommend that you read this SitePoint article by Daniel Gafitescu. He explains how to get started with the Symfony 2 Console Component with a simple example: generate all Fibonacci numbers between two given integers. Daniel also introduces essential concepts that we won’t go through today, so I highly suggest you have a look before moving on.

By the end of this post, we want to be able to create a basic console command to generate some output – any output will do – only the way to getting there is important. Near the end, we’ll discover some console helpers in order to create some nice interactions between users and the interface.

Continue reading this article on SitePoint

It will be great if we see a real sample on Progress bar, like when generating a bundle, or copying a big file. How will it work? By callbacks?

Yep, callbacks are the way to go. I would love a more in depth post about that, though, /hint hint

The Symfony installer shows a great example :smile:

Guzzle Download progress with the Progressbar.

I would actually recommend to have a look at https://github.com/webmozart/console which provides an alternative architecture for the Symfony Console command (Compatible with Symfony using Adapters).

Especially when building Console based applications as the Symfony Console components makes it much harder to properly separate service logic and keep things testable. (Thats the problem were are currently having for Gush)

1 Like