Jump to content

AppApi Module: AppApiPage


Sebi
 Share

Recommended Posts

AppApiPage adds the /page endpoint to the AppApi routes definition. Makes it possible to query pages via the api. 
This module relies on the base module AppApi, which must be installed before AppApiPage can do its work.

Route Description
/api/page/ Calls the root page of the page-tree
/api/page/42 Will call the page with id=42
/api/page/my/test/page Calls your page with path my/test/page

After installing AppApi and AppApiPage, you simply have to add the following code at the top of your ProcessWire-template to provide your page with a custom JSON output:

<?php
// Check if AppApi is available:
if (wire('modules')->isInstalled('AppApi')) {
  $module = $this->wire('modules')->get('AppApi');
  // Check if page was called via AppApi
  if($module->isApiCall()){
    // Output id & name of current page
    $output = [
      'id' => wire('page')->id,
      'name' => wire('page')->name
    ];

    // sendResponse will automatically convert $output to a JSON-string:
    AppApi::sendResponse(200, $output);
  }
}

// Here continue with your HTML-output logic...
  • Like 6
  • Thanks 1
Link to comment
Share on other sites

@flydev 👊🏻 Yep, that's right. The module has basically the same code as the PageApiAccess class that I posted in the other thread. I added an improvement for handling the "lang"-GET-param, that enables you to switch to another language in a multilang environment. But everything else is the same code.

 

  • Thanks 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...