Open fvsch opened 4 years ago
I love the idea and I think that it's completely obvious that we should have that.
The reason why you don't like page methods is that page methods and page models are made for two completely different use-cases:
site/models
directory without having to use a plugin.So again: It makes a lot of sense to add support for default models.
Regarding a few details:
autoload
site/models/default.php
We wouldn't be able to call it default.php
as that's already the model for the default
template (which is why the default controller is called site.php
).
However we also won't be able to call it site.php
as that should be reserved if we at some point decide that we should have support for a site model (i.e. a custom class extending the Kirby\Cms\Site
class).
So naming is a bit difficult here.
I wouldn't give plugins an explicit way to define a 'default' page model though.
Every part of Kirby that can be configured from the site
directory (except the config options themselves) can also be configured from plugins. I think we should do it like this here as well. Plugins always have the power to do weird things anyway, so it's the responsibility of plugin authors and users to use the features properly. I can see use-cases where a global default model defined from a plugin could make sense.
Page Models are IMO a better construct than custom page methods, but models always applying to a single page type can be frustrating. Would it be useful to have a built-in mechanism for defining a page model that applies to all classes?
I’ll try to explain my concern with page methods, what I’m doing with page models and how it might be improved with built-in support. It makes this post a bit long, which I apologize for in advance.
Custom page methods vs. page models
Page methods have a few downsides:
'projectname'
doesn't work, you need'somenamespace/projectname'
), copy-paste or write some boilerplate code.$this
object to access the page object, and editors cannot know what$this
refers to and provide completions and validation.Page
class, and when you try to do it, it fails silently.They’re a bit hidden away in the "Reference > Plugins" documentation, compared to Page Models which are described more proeminently in the Guide. The page methods docs also compares page models and page methods:
For these reasons, I tend to favor Page Models:
The only downside is that when you do want to add a method to all page types, you need to go back to plugin page methods.
A workaround for a 'base' page model
One way to share functionality between page models is to use class inheritance.
This works well and fixes all the issues mentioned before, but introduces two new issues:
site/models/common.php
(which can live anywhere)CommonPage
methods to be available for all page types, we have to create a page model that extendsCommonPage
for every single page type, even if it's an "empty" one (likeProductPage
in this example). In setups with half a dozen page types or more, that can be verbose.A built-in way to have a "base" or "default" page model?
Kirby has fallback mechanisms for:
site/templates/default.php
(required)site/templates/site.php
(optional)I propose introducing a fallback or "default" mechanism for page models as well.
For instance, Kirby could:
site/models/default.php
ArticlePage
class then for aDefaultPage
class.Developers could then decide to:
DefaultPage
class or not;Page
or extendDefaultPage
, depending on whether they want to inherit methods from DefaultPage or not.Downside: that's a bit magical. But the way page models are loaded and defined (with conventions for file names and class names) is already a bit magical, this would fit the current model and using 'default.php' as a fallback has some precedent with templates.
I wouldn't give plugins an explicit way to define a 'default' page model though. Doesn't feel right, but I haven't thought about it much.
What do you think? Good idea? Too complex or too magical?