$site undefined when trying routing

Hey,

So I’m trying to create profile pages, and after digging a little bit on the forums I’ve found some kind of answers based on other questions and I was trying to adapt my code. Unfortunately I’m getting a “site undefined” error.

Here’s my config routing code:

c::set('routes', array(
    array(
        'pattern' => 'profil/(:any)',
        'action'  => function($uid) {
          tpl::load(kirby()->roots()->templates() . DS . 'profile.php', array('uid' => $uid), false);
        }
    )
));

And here’s my profile.php page code:

    <?php snippet('header') ?>

    <div class="ctnr profiles">
        <div class="profile">
            <h1><?php echo $site->user($uid)->displayName() ?></h1>
            <p></p>
            <p></p>
            <p></p>
        </div>
    </div>
    <?php snippet('footer') ?>

I can’t find what I’m doing wrong :frowning:

Problem with using routes in this context is that you have to pass all the variables you need to the template.

c::set('routes', array(
    array(
        'pattern' => 'profil/(:any)',
        'action'  => function($uid) {
          $site = kirby()->site();
          tpl::load(kirby()->roots()->templates() . DS . 'profile.php', array('uid' => $uid, 'site' => $site), false);
        }
    )
));

Thanks, that makes sense, but even after this I’m still getting the error. I’ve posted a screenshot below:

This is pretty tricky internal stuff. You have to set the template variables globally, so they will also be available in snippets.

c::set('routes', array(
  array(
    'pattern' => 'profil/(:any)',
    'action'  => function($uid) {

      tpl::load(kirby()->roots()->templates() . DS . 'profile.php', [
        'site' => site(),
        'page' => page('profile'),
        'uid'  => $uid
      ], false);
    
    }
  )
));

If you have a “profile” page, which you just want to pass the UID to, you can also do this:

c::set('routes', array(
  array(
    'pattern' => 'profil/(:any)',
    'action'  => function($uid) {
      return ['profile', ['uid' => $uid]];
    }
  )
));
1 Like

Awesome, thanks so much! The second snippet worked like a charm!