Focus on IT Recommend

Home > Cakephp 3: how to implement events in helper

Cakephp 3: how to implement events in helper



in Cakephp 3 im trying to implement events in the helper class, this is example of what im trying to do:

protected $_View;

    public function __construct(View $View, $config = [])
        $this->_View = $View;

        parent::__construct($View, $config);

 * setup events
    protected function _setupEvents() {
        $events = [
            'filter' => [$this, 'filter'],

        foreach ($events as $callable) {
                $this->_View->eventManager()->on("Helper.Layout.beforeFilter", $callable);

    public function filter(&$content, $options = array()) {
            preg_match_all('/\[(menu|m):([A-Za-z0-9_\-]*)(.*?)\]/i', $content, $tagMatches);
            for ($i = 0, $ii = count($tagMatches[1]); $i < $ii; $i++) {
                    $regex = '/(\S+)=[\'"]?((?:.(?![\'"]?\s+(?:\S+)=|[>\'"]))+.)[\'"]?/i';
                    preg_match_all($regex, $tagMatches[3][$i], $attributes);
                    $menuAlias = $tagMatches[2][$i];
                    $options = array();
                    for ($j = 0, $jj = count($attributes[0]); $j < $jj; $j++) {
                            $options[$attributes[1][$j]] = $attributes[2][$j];
                    $content = str_replace($tagMatches[0][$i], $this->menu($menuAlias, $options), $content);
            return $content;

But im getting warning for the line where im calling constructor of parent Helper class:

Warning (4096): Argument 1 passed to App\View\Helper\MenusHelper::__construct() must be an instance of App\View\Helper\View, instance of App\View\AppView given, called in C:\wamp\www\CookieCMS\vendor\cakephp\cakephp\src\View\HelperRegistry.php on line 142 and defined [APP/View\Helper\MenusHelper.php, line 26]

Is it possible to implement events in helper this way and what is it that im doing wrong?

Recommend:Bootstrap pagination with CakePHP pagination helper

want my pagination elements to look like bootstrap's but generated by CakePHP. At the moment I've got this on my view page: <phpecho $this->Paginator->numbers(array( 'before' => '<div class="pagination"><ul>', 'separator' => '',

answer 1 >>---Accepted---Accepted---Accepted---

The Helper class is already implements EventlistenerInterface, so as explained in the manual all you have to do in your custom helper is return a proper array with required event name to callback mapping from implementedEvents() method.

So something like:

public function implementedEvents()
    $mapping = parent::implementedEvents();

    $mapping += [
        'Helper.Layout.beforeFilter' => 'someMethodOfYourHelper',

    return $mapping;

Recommend:CakePhp 3.0 - Missing Helper

gin's Helper. I actually tried to create some custom helpers for myself as well, but none has worked. I have the helper files saved in app/src/View/Helper/ (my custom helper) and app/plugins/UploadPack/src/View/Helper/ (the plugin's helper,

------splitte line----------------------------