Database functions: controller vs model vs plugin

moinmoin from kiel.
Here is another silly question from a newbie :wink:

I’m creating an intranet-app and I need to access a database. After having read the posts about controllers and models I have come to a decision and now I want to know if you pro’s agree.

The app needs database-access on different pages with different tasks. There are actually 2 pages that share the exact same database-functionality but for different user-roles. That would in my understanding best be handled with a shared controller. But the rest of the pages need a different though partially overlapping set of db-functions.

And for the sake of simplicity concerning maintenance I would like to keep the database-functionality in one place.

Now: would that place be a plugin?

And is this information (from 2016) still correct?:

If so, would it be enough to put an index.php in the plugin-directory(/site/plugins/myDBplugin/)?

Thanks for all the help :+1:

Yes, anything you put into an index.php in a plugin folder is accessible from your templates, snippets controllers etc.

Oh, thank you.

That means, if I put a function like that:

function hello(){
	return "hello world";

in site/plugins/hello_world/index.php, it will show up when I print dump($kirby->plugins()) ?

And how would I access this function?

Not the function will show up, but the plugin when you dump($kirby->plugins()).

You can then use the function like a function.

echo hello();

Thank you for the information!

So to see the plugin when I dump the plugins-object, I need to set an author and a name:

Kirby::plugin('me/myPlugin', [ ]);

function hello()
	return "hello world";

But to just use the function, it’s enough to call the function-name in a template.

It’s all in the docs, of course, but I only can make sense of it, when I actually need it and someone makes me see …

Thank you for the patience :pray: