# Plugin controller for block plugin

**URL:** <https://forum.getkirby.com/t/plugin-controller-for-block-plugin/29339>\
**Category:** Questions\
**Tags:** v3\
**Created:** [August 21, 2023, 1:05pm UTC](https://forum.getkirby.com/t/plugin-controller-for-block-plugin/29339 "2023-08-21T13:05:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![samcity](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@samcity](https://forum.getkirby.com/u/samcity)\
**Post date:** [August 21, 2023, 1:05pm UTC](https://forum.getkirby.com/t/plugin-controller-for-block-plugin/29339/1 "2023-08-21T13:05:32Z")

</div>

Hi community,  
I am working on a block plugin and would like to handle the logic in a controller instead inside the snippet. My plugin structure looks like this:

myplugin  
– blueprints  
---- blocks  
------ myplugin.yml  
– controllers  
---- blocks  
------ myplugin.php  
– snippets  
---- blocks  
------ myplugin.php  
– composer.json  
– index.js  
– index.php

index.php:

```auto
<?php

Kirby::plugin('me/myplugin', [
  'blueprints' => [
    'blocks/myplugin' => __DIR__. '/blueprints/blocks/myplugin.yml',
  ],
  'snippets' => [
    'blocks/myplugin' => __DIR__. '/snippets/blocks/myplugin.php',
  ],
  'controllers' => [
    'blocks/myplugin' => require __DIR__. '/controllers/blocks/myplugin.php',
]
]);

```

controllers/blocks/myplugin.php:

```auto
<?php
return function ($site) {
    return [
        'myVar' => $site->title();
    ];
}; 

```

snippets/blocks/myplugin.php:

```auto
<h1><?= $myVar ?></h1>

```

The usage of $myVar in snippets/blocks/myplugin.php throws an error saying: “Undefined variable for myVar”.

Any idea on how to use the controller properly?

---

<div class="post-metadata">

**Author:** ![pixelijn](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@pixelijn](https://forum.getkirby.com/u/pixelijn)\
**Post date:** [August 21, 2023, 1:21pm UTC](https://forum.getkirby.com/t/plugin-controller-for-block-plugin/29339/2 "2023-08-21T13:21:09Z")

</div>

There is no such thing as a block controller. Where did you get that from?

---

<div class="post-metadata">

**Author:** ![ahmetbora](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/ahmetbora/32/11671_2.png) [@ahmetbora](https://forum.getkirby.com/u/ahmetbora)\
**Post date:** [August 21, 2023, 1:52pm UTC](https://forum.getkirby.com/t/plugin-controller-for-block-plugin/29339/3 "2023-08-21T13:52:56Z")

</div>

You can try block models:

> **[Block models](https://getkirby.com/docs/reference/plugins/extensions/block-models)**

```php
use Kirby\Cms\Block;

class HeadingBlock extends Block
{
    public function customMethod(): string
    {
        return '<h1>Custom HTML for headings</h1>';
    }
    
    public function controller(): array
	{
		return [
			'myVar' => site()->title(),
            
			'block' => $this,
			'content' => $this->content(),
			// deprecated block data
			'data' => $this,
			'id' => $this->id(),
			'prev' => $this->prev(),
			'next' => $this->next()
		];
	}
}

Kirby::plugin('my/blockModels', [
    'blockModels' => [
        'heading' => HeadingBlock::class
    ]
]);

```

You can override block controller method:

> <https://github.com/getkirby/kirby/blob/3.9.6.1/src/Cms/Block.php#L126-L137>

---

<div class="post-metadata">

**Author:** ![samcity](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@samcity](https://forum.getkirby.com/u/samcity)\
**Post date:** [August 21, 2023, 4:53pm UTC](https://forum.getkirby.com/t/plugin-controller-for-block-plugin/29339/4 "2023-08-21T16:53:16Z")

</div>

I don’t remember where i saw it first or if i came up with it. 😅  
Wouldn’t it be logical to have sth like a plugin controller to keep the logic out of the plugin snippets?
