Skip to content

Commit a873481

Browse files
committed
Update command_in_controller.rst
with real world example about Swift Mailer.
1 parent e343ac8 commit a873481

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed
Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,42 @@
11
.. index::
22
single: Console; Use commands in your Controller
33

4-
How to make use of a Command in your application
5-
================================================
4+
How to Call a Command from a Controller
5+
=======================================
66

77
The :doc:`Console component documentation </components/console/introduction>`_ covers
88
how to create a console command. This cookbook article covers how to use a console command
9-
directly in your application.
9+
directly in your application.
1010

11-
Execute Command directly from Controller
12-
----------------------------------------
11+
You may have the need to execute some function that is only available in a console command.
12+
Usually, you should refactor the command and move some logic into a service that can be
13+
reused in the controller. However, when the command is part of a third-party library, you
14+
wouldn't want to modify or duplicate their code, but want to directly execute the command
15+
instead.
1316

14-
Symfony allows you to directly execute a ``Command`` inside your Controller::
17+
An example of this is sending the emails that Swift Mailer spooled earlier
18+
:doc:`using the ``swiftmailer:spool:send`` command </cookbook/email/spool>`_. Symfony
19+
allows you to directly execute a registered ``Command`` inside your Controller::
1520

16-
// src/AcmeBundle/Controller/GreetController.php
21+
// src/AcmeBundle/Controller/SpoolController.php
1722
namespace AcmeBundle\Controller;
1823

1924
use Symfony\Component\Console\Output\StreamOutput;
2025
use Symfony\Component\Console\Input\ArrayInput;
2126
use Symfony\Bundle\FrameworkBundle\Console\Application;
2227

23-
class GreetController
28+
class SpoolController
2429
{
25-
public function greetAction($name)
30+
public function sendSpoolAction($messages=10)
2631
{
2732
$kernel = $this->get('kernel');
2833
$application = new Application($kernel);
2934
$application->setAutoExit(false);
3035

31-
$input = new ArrayInput(array('command' => 'acme:greet'));
36+
$input = new ArrayInput(array(
37+
'command' => 'swiftmailer:spool:send',
38+
'--message-limit' => $messages,
39+
));
3240
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
3341
$application->run($input, $output);
3442

@@ -40,30 +48,33 @@ Symfony allows you to directly execute a ``Command`` inside your Controller::
4048
}
4149
}
4250

43-
Color code the Ansi content to HTML
44-
----------------------------------------
51+
Showing Colorized Command Output
52+
--------------------------------
4553

46-
By telling the ``StreamOutput`` it is decorated via the third parameter, it will return the Ansi color-coded content.
47-
The `SensioLabs AnsiToHtml converter`_ can be required using ``Composer``
48-
and helps you to get colorful html::
54+
By telling the ``StreamOutput`` it is decorated via the third parameter, it will return
55+
the Ansi color-coded content. The `SensioLabs AnsiToHtml converter`_ can be required
56+
using ``Composer`` and helps you to get colorful html::
4957

50-
// src/AcmeBundle/Controller/GreetController.php
51-
namespace AcmeBundle\Controller;
58+
// src/AppBundle/Controller/SpoolController.php
59+
namespace AppBundle\Controller;
5260

5361
use Symfony\Component\Console\Output\StreamOutput;
5462
use Symfony\Component\Console\Input\ArrayInput;
5563
use Symfony\Bundle\FrameworkBundle\Console\Application;
5664
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
5765

58-
class GreetController
66+
class SpoolController
5967
{
60-
public function greetAction($name)
68+
public function sendSpoolAction($messages=10)
6169
{
6270
$kernel = $this->get('kernel');
6371
$application = new Application($kernel);
6472
$application->setAutoExit(false);
6573

66-
$input = new ArrayInput(array('command' => 'acme:greet'));
74+
$input = new ArrayInput(array(
75+
'command' => 'swiftmailer:spool:send',
76+
'--message-limit' => $messages,
77+
));
6778
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL, true);
6879
$application->run($input, $output);
6980

@@ -78,8 +89,8 @@ and helps you to get colorful html::
7889
}
7990
}
8091

81-
The ``AnsiToHtmlConverter`` can also be registered
82-
`as a Twig Extension`_, and supports optional themes
92+
The ``AnsiToHtmlConverter`` can also be registered `as a Twig Extension`_,
93+
and supports optional themes.
8394

8495
.. _`SensioLabs AnsiToHtml converter`: https://github.com/sensiolabs/ansi-to-html
8596
.. _`as a Twig Extension`: https://github.com/sensiolabs/ansi-to-html#twig-integration

0 commit comments

Comments
 (0)