-
-
Notifications
You must be signed in to change notification settings - Fork 10
4.0: AppApi Modules
Since version 1.2.0 it is possible to install AppApi modules that provide their own route handlers without having to change anything in Routes.php.
Basically, the way to define endpoints in Routes.php is the easier and most practical way. But if you are implementing a generic endpoint that you may want to reuse in other ProcessWire instances, an installable module can be very handy. Updates are then easier to distribute and you don't have to copy your code everywhere.
I would be very happy if you would also provide practical endpoints as modules for the community! Let me know so I can add your great AppApi module to the list!
Here is a list of the currently available AppApi modules:
-
AppApi File
Adds the /file endpoint to your routes definition. Makes it possible to query files via the api. -
Twack
Adds the /tpage endpoint to your route definition. Twack is a framework which enables you to create your ProcessWire-templates with reusable and nest-able components. /tpage lets you call the JSON endpoint of those components.
Writing your own AppApi module is actually not complicated at all! Take a look at the code below. It's an installable and fully functional AppApi module! I have added comments in the code explaining how it works. Take the module as a template and implement your own endpoint with it.
<?php
namespace ProcessWire;
/**
* AppApiHelloWorld adds the /hello-world endpoint to the AppApi routes definition.
*/
class AppApiHelloWorld extends WireData implements Module {
public static function getModuleInfo() {
return [
// Change the following infos to your own texts:
'title' => 'AppApi - Hello World',
'summary' => 'AppApi-Module that demonstrates how to add a simple module-endpoint.',
'version' => '1.0.0',
'author' => 'Sebastian Schendel',
'icon' => 'terminal',
'href' => 'https://modules.processwire.com/modules/app-api/',
'requires' => [
'PHP>=7.2.0',
'ProcessWire>=3.0.98',
'AppApi>=1.2.0' // You need AppApi in v1.2.0 or higher!
],
'autoload' => true,
'singular' => true
];
}
/**
* The init() function will get called automatically by ProcessWire
*/
public function init() {
$module = $this->wire('modules')->get('AppAPI');
// Register your custom route-definition:
$module->registerRoute(
'hello-world', // endpoint-name
[
['OPTIONS', '', ['GET']],
['GET', '', AppApiHelloWorld::class, 'sayHello'] // Calls the static function sayHello in the module class
]
);
}
public static function sayHello($data) {
// This will be returned as JSON if the endpoint is called
return [
'success' => true,
'message' => 'Hello World!'
];
}
}
If make a GET-Request to /api/hello-world
, it will simply output:
{
"success": true,
"message": "Hello World!"
}
AppApi
Connect your apps to ProcessWire!
ProcessWire-Module: | https://modules.processwire.com/modules/app-api/ |
Support-Forum: | https://processwire.com/talk/topic/24014-new-module-appapi/ |
Repository: | https://github.com/Sebiworld/AppApi |
Wiki: | https://github.com/Sebiworld/AppApi/wiki |