Error when extending model that succeeds current model

This is what my contact model looks like:

<?php

class ContactPage extends DefaultPage {
...
}

default model:

<?php

class DefaultPage extends Page {
...
}

and the models folder structure:

> models
- contact.php
- default.php

I figured the system works alphabetically, though I didn’t have issues doing this back in Kirby 2. Is this a bug? Is there a workaround besides arranging it alphabetically?

You can require the model php in your contact model, i ran into that the other day as well

The use of @agathago mentions that I also use it very often. Using models/0.php :smile:
Would it be nice to define a default model?

Is it possible to namespace DefaultPage and autoload it?[1]

right now I’m using something like this as adviced here.

load([
	'DefaultPage' => 'default.php'
], __DIR__);

class MyCustomPage extends DefaultPage {}

[1] This didn’t work or some reason?!

composer.json

    "autoload": {
      "psr-4": {
        "MO\\": "site/"
      }
    }

site/models/default.php

namespace MO\models;

class DefaultPage extends Kirby\Cms\Page {}

site/models/my-custom.php

class MyCustomPage extends MO\models\DefaultPage {}

I’m no expert at this, but I believe PSR-4 expects the filename to have the same name as the loaded class name.
So, I think, if you expect PSR-4 to load \MO\models\DefaultPage, the filename should be site/models/DefaultPage.php, but that of course won’t work with Kirby which expects default.php.

That might be the cause. Don’t know if there’s a solution.

1 Like

You are correct, its the filename. Thanks :slight_smile:

Maybe you could alias the DefaultPage class to “default” and then load MO\models\default instead.

site/models/default.php

namespace MO\models;

class DefaultPage extends Kirby\Cms\Page {
    // default stuff here
}
class_alias('MO\models\DefaultPage', 'MO\models\default');
// I think you need to specify namespaces, no sure tho... 

site/models/my-custom.php

class MyCustomPage extends MO\models\default {}