Synfony3 Console: Add own helper to HelperSet

267 views Asked by At

Is there a way to add own helpers to the console HelperSet in Symfony3?

I didn't find any helpful thing in the documentation.

1

There are 1 answers

0
freesh On

Ok i followed the code and find a simple solution. :)

I just have to add my class that implements the HelperInterface, or extend the abstract Helper class.

$this->getHelperSet()->set(new MyHelper(), 'myhelper');

And myhelper class looks like that:

<?php
namespace MyApp\Helper;

use Symfony\Component\Console\Helper\Helper;

class MyHelper extends Helper
{
    /**
     * @param $string
     * @return string
     */
    public function doIt($string) {
        return 'this is your '.$string;
    }

    /**
     * Returns the canonical name of this helper.
     *
     * @return string The canonical name
     */
    public function getName() {
        return 'myhelper';
    }
}

And in my code i can use it like:

$myhelper = $this->helperSet->get('myhelper');
$myString = $myhelper->doIt('hallo');

:)