How to Load Page Models in a Plugin

We’re writing a plugin that is going create Virtual Pages from a third-party API. We’re using a few different Page Models, and thought that if we created a /models folder inside the plugin folder it would be picked up automagically by Kirby. But apparently, it isn’t.

Our plugin directory has folders like this:

myplugin/
    index.php
    blueprints/
    templates/
    models/
        products.php
        product.php
    ....

Inside index.php, we are trying to load the Page Models as shown in the docs:

...
'pageModels' => [
    'product' => 'ProductPage',
    'products' => 'ProductsPage'
]
...

…but the Page Model classes don’t load.

We experimented and seem to have found that we need to explicitly include the ‘models’ files before declaring the ‘pageModels’ - for example, like this:

'models' => [
    'product' => include(__DIR__ . '/models/product.php'),
    'products' => include(__DIR__ . '/models/products.php')
],
'pageModels' => [
    'product' => 'ProductPage',
    'products' => 'ProductsPage'
]

If we merely ‘include’ the model files without also declaring ‘pageModels’, it doesn’t work either. We seem to need to do both: include the ‘models’ files, then declare the ‘pageModels’ in the plugin’s index.php. Is that how it’s supposed to be done?

I haven’t been able to find an example in the docs that shows where to save model files for a plugin, or any mention that we need to explicitly load them. So, I am wondering whether this is the ‘kirby way’ to do this, or whether we’ve overlooked something really obvious…

Yes, I’m also doing it like this here: Virtual pages from image gallery | Kirby CMS