I want a page per year, displaying blog posts from that year, but am failing at the first hurdle – when I visit the URL for the year, I get an error from the route:
Call to a member function render() on null
My blog posts are in a structure like:
blog/
2023/
01/
31/
1_my-post/
post.txt
Posts should be visible at urls like /blog/2023/01/31/my-post/
.
I have this in my config.php
:
return [
'routes' => [
[
'pattern' => 'blog/(\d{4})',
'action' => function ($year) {
$data = [
'year' => $year
];
# THIS LINE GENERATES THE ERROR:
return page('year')->render($data);
}
]
]
]
I’ve added a template at site/templates/year.php
, a model at site/models/year.php
, a blueprint at site/blueprints/pages/year.yml
, and a controller at site/controllers/year.php
.
Model:
<?php
class YearPage extends Page {
}
Controller:
<?php
return function ($page, $year) {
return [
'year' => $year
];
};
I’ve obviously misunderstood something simple, or made a typo, because I can’t see how to have page('year')
in the route not be NULL.