1
1
.. index ::
2
2
single: Console; Use commands in your Controller
3
3
4
- How to make use of a Command in your application
5
- ================================================
4
+ How to Call a Command from a Controller
5
+ =======================================
6
6
7
7
The :doc: `Console component documentation </components/console/introduction >`_ covers
8
8
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.
10
10
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.
13
16
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::
15
20
16
- // src/AcmeBundle/Controller/GreetController .php
21
+ // src/AcmeBundle/Controller/SpoolController .php
17
22
namespace AcmeBundle\Controller;
18
23
19
24
use Symfony\Component\Console\Output\StreamOutput;
20
25
use Symfony\Component\Console\Input\ArrayInput;
21
26
use Symfony\Bundle\FrameworkBundle\Console\Application;
22
27
23
- class GreetController
28
+ class SpoolController
24
29
{
25
- public function greetAction($name )
30
+ public function sendSpoolAction($messages=10 )
26
31
{
27
32
$kernel = $this->get('kernel');
28
33
$application = new Application($kernel);
29
34
$application->setAutoExit(false);
30
35
31
- $input = new ArrayInput(array('command' => 'acme:greet'));
36
+ $input = new ArrayInput(array(
37
+ 'command' => 'swiftmailer:spool:send',
38
+ '--message-limit' => $messages,
39
+ ));
32
40
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL);
33
41
$application->run($input, $output);
34
42
@@ -40,30 +48,33 @@ Symfony allows you to directly execute a ``Command`` inside your Controller::
40
48
}
41
49
}
42
50
43
- Color code the Ansi content to HTML
44
- ----------------------------------------
51
+ Showing Colorized Command Output
52
+ --------------------------------
45
53
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::
49
57
50
- // src/AcmeBundle /Controller/GreetController .php
51
- namespace AcmeBundle \Controller;
58
+ // src/AppBundle /Controller/SpoolController .php
59
+ namespace AppBundle \Controller;
52
60
53
61
use Symfony\Component\Console\Output\StreamOutput;
54
62
use Symfony\Component\Console\Input\ArrayInput;
55
63
use Symfony\Bundle\FrameworkBundle\Console\Application;
56
64
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
57
65
58
- class GreetController
66
+ class SpoolController
59
67
{
60
- public function greetAction($name )
68
+ public function sendSpoolAction($messages=10 )
61
69
{
62
70
$kernel = $this->get('kernel');
63
71
$application = new Application($kernel);
64
72
$application->setAutoExit(false);
65
73
66
- $input = new ArrayInput(array('command' => 'acme:greet'));
74
+ $input = new ArrayInput(array(
75
+ 'command' => 'swiftmailer:spool:send',
76
+ '--message-limit' => $messages,
77
+ ));
67
78
$output = new StreamOutput(tmpfile(), StreamOutput::VERBOSITY_NORMAL, true);
68
79
$application->run($input, $output);
69
80
@@ -78,8 +89,8 @@ and helps you to get colorful html::
78
89
}
79
90
}
80
91
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.
83
94
84
95
.. _`SensioLabs AnsiToHtml converter` : https://github.com/sensiolabs/ansi-to-html
85
96
.. _`as a Twig Extension` : https://github.com/sensiolabs/ansi-to-html#twig-integration
0 commit comments