Custom route to alternative template

Hi,

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

/events/event-name

and at the custom route

/events/event-name/media

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,
Till

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.
    site/models/event.php
    <?php
    
     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/event-name.media. For this, just create a template with the name event.media.php.
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!