Using site variables within functions

I’m trying to use site variables such as $site, $pages and such within a function but it just isn’t working. I’ve managed to get around this by passing them in from my snippet file which is calling the functions eg myfunction($site, $pages) but now I am trying to use routes also it doesn’t seem to work.

What is the scope of using these within routes or how would i use them.

At present i am trying to use the below code to no avail, once again trying to put the $pages var in as a variable purely because of my frustration.

c::set('routes', array(
  array(
    'pattern' => 'ajax/comments',
    'action' => function () {
            checkComments($_GET, $page);
    }
  )
));

In the router $page is obviously not defined - you are on a custom route, not on any given page.

You can get access to a page object like that:

c::set('routes', array(
  array(
    'pattern' => 'ajax/comments',
    'action' => function () {
        $page = pages('your/page');
        checkComments($_GET, $page);
    }
  )
));

Also note that there are functions site(), page() and pages() that will work in many contexts and return the respective object.

Okay well I’ve tried using
> c::set(‘routes’, array(
> array(
> ‘pattern’ => ‘ajax/comments’,
> ‘action’ => function () {
> $page = page($_GET[‘page’]);
> dump($page);
> //checkComments($_GET, $page);
> }
> )
> ));

with $_GET[‘page’]) being the page id but the dumps nothing

What version of kirby are you running, and what is your query string?

This code works perfectly fine on my starterkit-installation with kirby 2.2.2.

Using 2.2.2 and the query string is:

ajax/comments?page=how-to-become-internet-famous-for-68&offset=0&start=0

You have to pass the URI of the page instead of the UID to the page function. Your page paramater should probably be something like blog/article/how-to.....

When you have slashes in your parameter, use urlencode and urldecode, respectively (http://php.net/manual/de/function.urlencode.php).

Thanks so much! URL encoding has done the trick, now to debug some other issues haha