JSON of all pages

Im trying to clean up an old bit functionality in my template, because, well, its old and theres a few to many moving parts for my liking. It works, but it works the way described in the cookbook which I think is a bit of an old way now.

Basically the whole point is to generate a json file of all the pages in the site so I can feed it to Uncss so that it can optimise the CSS for unused code.

Right now, I have a page template, and content file, and a blue print that hides the page in the panel. if i go to domain.dev/uncss i get the JSON in the browser. I use curl to save the this to a file on the file system and then pump that into uncss… it works but to many moving parts.

how can i clean up, remove stuff, and have it so that on a local domain i can visit domain.dev/uncss.json and feed that directly to uncss without have to save the file via curl and have the template and content files?

I read the load more from ajax guide, but thats just confused me further :slight_smile:

My page template looks like this, because Uncss is expecting a particular format to the JSON…

<?php
header('Content-type: application/json; charset=utf-8');
$root = $site->find('home');
$firstlevel = $site->children()->visible();
$secondlevel = $site->children()->children()->visible();
$thirdlevel = $site->children()->children()->children()->visible();
$json = array();
$json[] =  (string)$root->url();

foreach ($firstlevel as $article) {
    $json[] =  (string)$article->url();
}

foreach ($secondlevel as $article) {
    $json[] =  (string)$article->url();
}

foreach ($thirdlevel as $article) {
    $json[] =  (string)$article->url();
}

echo json_encode($json);

How do i convert this into the way described above?

What do you mean, have the template and content files? From what I see in your JSON, all you do is generate a list of URLs. One other thing I don’t understand is why you generate all levels separately.

You don’t need a page for the JSON API, you can just provide that list via a route.

Oh. Well it looks like i figured it was way complicated then it turned out to be. I though from reading the stuff in the cookbook that i would need controllers and stuff. The old code used levels because thats how it ended up after asking in the forum along time ago :slight_smile:

Turns out this was enough:

c::set('routes', array(
  array(
    'pattern' => 'uncss.json',
    'action'  => function() {
      header('Content-type: application/json; charset=utf-8');
      $pages = site()->index()->visible();
      $json = array();

      foreach ($pages as $article) {
          $json[] = (string)$article->url();
      }
      echo json_encode($json);
    }
  )
));

p.s. … why do you have a little cake beside your name today? Is it a special day?

Yes, very special, it says it is the anniversary of the day I joined this wonderful community!

1 Like

Actually, it wasn’t enough. The index page is missing. I have solved it like this, unless there is a better way?

c::set('routes', array(
  array(
    'pattern' => 'uncss.json',
    'action'  => function() {
      header('Content-type: application/json; charset=utf-8');
      $pages = site()->index()->visible();
      $root = site()->find('home');
      $json = array();
      $json[] =  (string)$root->url();

      foreach ($pages as $page) {
          $json[] = (string)$page->url();
      }
      echo json_encode($json);
    }
  )
));

Well, thats lovely :slight_smile: Congratulations. :tropical_drink:

You, the home page is not visible, that’s probably the reason.