Download link for each subpage

Hi everyone,
I am currently trying to set a route to force the download of a pdf on many different subpages.
Each subpage/project will have its own pdf. In the template I’d like to link to “index/projectname/download”.

How do I set the route for this?
So far I’ve got the following:

c::set('routes', array(
  array(
    'pattern' => 'index/(:any)/download',
    'action'  => function() {

      page()
        ->files()
        ->filterBy('extension', 'pdf')
        ->first()
        ->download();
    }
  )
));

I believe I need to tell kirby to fetch the current page’s pdf, but I have no idea how to.

Thanks!

You can get the project page like this:

c::set('routes', array(
  array(
    'pattern' => 'index/(:any)/download',
    'action'  => function($uri) {
      $page = page($uri);
      $page
        ->files()
        ->filterBy('extension', 'pdf')
        ->first()
        ->download();
    }
  )
));
1 Like

The example above works if the page is on the top level of the site. If your projects are all subpages of a specific page, you can get the page like this:

$page = page('projects/' . $uri);
1 Like

thanks guys, works like a charm!