#1 April 17, 2015 6:07pm

dmont
Member
Registered: April 17, 2015
Posts: 3

BigTree as an API

I'm moving an app from a rails backend to (hopefully) BigTree. The app just needs json and static content, but hosting changes demand php.  BigTree has a nice admin interface and cuts out the frills that come with a lot of other CMSes; I'm hoping to use BigTree's admin interface with the already-built front end.

I've got my fingers crossed this is something BigTree can take in stride. Since I don't know the internals too well, some confirmation or direction would be helpful:

Right now my plans are to put BigTree behind `/api` in BigTree's www_root configuration and serve up the app's index page to anything that's not `^/api`
Are the any attendant pitfalls I should watch out for on the BigTree side of things?

Last edited by dmont (April 17, 2015 6:08pm)

Offline

#2 April 17, 2015 8:32pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 970

Re: BigTree as an API

The only things to watch out for are the reserved paths here:
https://www.bigtreecms.org/docs/dev-gui … ial-paths/

Honestly, if you're just going to use BigTree as a JSON feeder you can probably setup .htaccess to ignore most of the reserved paths. You can setup the admin to be routed from whatever route you want via $bigtree["config"]["admin_root"]. Then you can just setup a routed template to drive /api/ and feed your front end straight JSON data or whatever your plan may be.

Happy to be a sounding board if you need any more ideas on implementing it -- we've never done a front end that feeds directly on JSON or XML data from BigTree but we have done things that are very app-like before that are run through a single routed template. You could also just set the homepage to be the routed template and actually put BigTree in the /api/ folder. That would keep all of its routing logic contained outside the front end app.

Offline

#3 June 21, 2015 1:15am

jbcoder
Member
Registered: June 21, 2015
Posts: 1

Re: BigTree as an API

I'm pretty new to BigTree (as I just decided to use it for a personal project about a week ago). I started off programming in PHP and then found myself in love with AngularJS as a front-end framework. Now since I do front and back end work, I would like to see how I can use this to create a feed for some things that I'd like to use AngularJS for. So, similar to the OP, I'm wondering what the best way would be to serve up JSON data in an API like way from BigTree?

Offline

#4 June 21, 2015 8:38pm

timbuckingham
Administrator
From: Baltimore, MD
Registered: April 2, 2012
Posts: 970

Re: BigTree as an API

There's no official JSON feed support at the moment in BigTree, though it is in the pipeline for the 4.3 release (see https://www.bigtreecms.org/docs/dev-guide/feeds/ for the Feeds docs). The best way to do it at the moment is to just create a routed template you want to use to generate the JSON and assign it to a page.

Say you have a news module that has news items and news categories. You create a routed template, we'll say "json" is the ID. You then assign it to a Page in the Pages tree called "JSON" so it now lives at http://www.website.com/json/.

Now we'll create two files in the /templates/routed/json/ folder:
- news.php
- categories.php

In news.php:

<?php
    $newsMod = new MyNewsModuleClass;
    $news = $newsMod->getRecent(10);
    echo json_encode($news);

In categories.php:

<?php
    $catMod = new MyCategoryClass;
    $categories = $catMod->getAllPositioned();
    echo json_encode($categories);

Just some quick examples of how you might set it up! Obviously in the news.php file you might want to add support for POST or GET vars to search by category or change the sorting or something of that nature.

Offline

#5 November 5, 2015 3:58pm

randyhook
Member
Registered: February 25, 2015
Posts: 6

Re: BigTree as an API

Just wanted to share how we've been using BigTree as an API in a recent project.

We do work for a client whose site runs on another CMS, and we have no access to the site with the exception of a place we can drop some JavaScript. The challenge was to start collecting user data based on products they were viewing to not only store it for analysis, but to also give the user some extra pieces to interact with when they return to the site.

Here is an overview of the pieces involved:
1. We wrote some JavaScript that we place inside of Google Tag Manager, which also houses other marketing scripts we use.
2. Google Tag Manager gets injected on the third-party site.
3. In this script we work with user data via cookies, but we also make ajax calls to a BigTree site we developed that sits under our control. Let's call it api.mysite.com.
4. The script makes calls like api.mysite.com/api/v1/store-user-product and api.mysite.com/api/v1/get-user-products.
5. Our site has a routed template at api.mysite.com/api/v1/default.php.
6. Default.php looks at the commands and delegates the tasks to the appropriate modules and returns JSON to the caller.
7. The BigTree site has a site setting called Allowed Referrers that default.php consults to make sure the caller has authorization to interact with the API.

There were a few challenges, such as learning to use JSONP in the AJAX calls to work around the cross-origin requests, but it has been really working great. Our next step is to make the BigTree site more robust so that it can be our hub for all of our web service calls.

Offline

Board footer

Powered by FluxBB

The Discussion Forum is not available on displays of this size.