Browse Source

include custom routes for api

CauseFX 5 years ago
parent
commit
95392ffbb6
3 changed files with 35 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 6 0
      api/v2/index.php
  3. 27 0
      api/v2/routes/custom/index.html

+ 2 - 0
.gitignore

@@ -118,6 +118,8 @@ plugins/plugin_files/*
 !plugins/plugin_files/index.html
 plugins/images/userTabs/*
 !plugins/images/userTabs/index.html
+api/v2/routes/custom/*
+!api/v2/routes/custom/index.html
 # =========================
 # Plugin files
 # =========================

+ 6 - 0
api/v2/index.php

@@ -96,6 +96,12 @@ $app->add(function ($request, $handler) {
 foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
 	require_once $filename;
 }
+/*
+ * Include all custom routes
+ */
+foreach (glob(__DIR__ . DIRECTORY_SEPARATOR . 'routes' . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . '*.php') as $filename) {
+	require_once $filename;
+}
 /*
  * Include all Plugin routes
  */

+ 27 - 0
api/v2/routes/custom/index.html

@@ -0,0 +1,27 @@
+Place all custom route files here in this directory....
+
+Name file something anything you like...
+<pre>
+&lt;?php
+
+    /*
+     * The first thing you need to edit it the <code>get</code> part - options are get/post/delete/put/options
+     * The second thing you need to edit is the </code>/something</code>
+     * This will be the endpoints name and will be accessible from: http://organizr/api/v2/custom/something
+     */
+
+    $app->get('/custom/something', function ($request, $response, $args) {
+        // Let's define the Organizr Class to the $Organizr variable
+        $Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+        // Now let's set auth on our function, 1 is for co-admin and upto 999 is for Guest
+        if ($Organizr->qualifyRequest(1, true)) {
+            // Let's assign the api response with our function that holds our data...
+            $GLOBALS['api']['response']['data'] = $Organizr->getAllUsers();
+        }
+        // You do not need to change anything else below this line
+        $response->getBody()->write(jsonE($GLOBALS['api']));
+        return $response
+            ->withHeader('Content-Type', 'application/json;charset=UTF-8')
+            ->withStatus($GLOBALS['responseCode']);
+    });
+</pre>