Custom route to alternative template


I’m trying to create a custom route that shows an alternative template to a page. The original page is at the url


and at the custom route


I want to show some fields from the page in a different layout. I got it to work, like this:

'routes' => [
      'pattern' => 'events/(:any)/media',
      'action'  => function ($name) {
        return new Page([
          'slug' => $name,
          'template' => 'event-media',
          'content' => page('events/' . $name)->toArray()['content']

but I was wondering if this actually the best way to do it.

Thanks in advance,

I think its a very valid solution. :slight_smile:

Two other methods could be:

  1. have a page model for the event, where you override the children method to return a media child.
     class EventPage extends Page {
         public function children() {
             return Pages::factory([
                 'slug' => 'media',
                 'template' => 'event-media'
                 'content' => $this->content()
             ], $this);
  2. Have a content representation template.
    This however changes the route to /events/ For this, just create a template with the name
1 Like

To prevent duplicate content, you might want to make the original URL the canonical one.

1 Like

Thanks a lot to the both of you!