XF 2.1 Correct way to add custom endpoints to the API.

Hello,

I'm trying to extend the API to provide some further integration with our own site, and I'm running into a few issues.

I get this error when trying to access the route.

JSON:
{
    "errors": [
        {
            "code": "endpoint_not_found",
            "message": "Requested endpoint cannot be found.",
            "params": {
                "reason": "invalid_route",
                "debug_reason": "invalid_controller",
                "debug_controller": "RCRP\\API:User",
                "debug_action": null
            }
        }
    ]
}

Here is the controller

PHP:
<?php

namespace RCRP\API\XF\Api\Controller;

use XF\Mvc\ParameterBag;

class User extends XFCP_User {

    public function actionGet(ParameterBag $parameterBag) {
       return $this->apiResult([
           'test' => true
       ]);
    }
}

The class extension:

JSON:
{
    "from_class": "XF\\Api\\Controller\\AbstractController",
    "to_class": "RCRP\\API\\XF\\Api\\Controller\\User",
    "execute_order": 10,
    "active": true
}

The class hint:

PHP:
<?php

// ################## THIS IS A GENERATED FILE ##################
// DO NOT EDIT DIRECTLY. EDIT THE CLASS EXTENSIONS IN THE CONTROL PANEL.

namespace RCRP\API\XF\Api\Controller
{
   class XFCP_User extends \XF\Api\Controller\AbstractController {}
}

The route file generated:

JSON:
{
    "route_type": "api",
    "route_prefix": "rcrp",
    "sub_name": "",
    "format": "",
    "build_class": "",
    "build_method": "",
    "controller": "RCRP\\API:User",
    "context": "",
    "action_prefix": ""
}

Any help would be great.
 
I've managed to do it. It was just a bit of a trial and error. Here is my config:

Route json file:

JSON:
{
    "route_type": "api",
    "route_prefix": "emailbounce",
    "sub_name": "",
    "format": "",
    "build_class": "",
    "build_method": "",
    "controller": "AlterSi\\CustomMods:EmailBounce",
    "context": "",
    "action_prefix": ""
}


Controller PHP Example:
PHP:
<?php
namespace AlterSi\CustomMods\Api\Controller;

use XF\Api\Controller\AbstractController;
use XF\Mvc\ParameterBag;
use XF\Mvc\Reply\AbstractReply;
use XF\Mvc\RouteMatch;

use XF\Mvc\Entity\Entity;


/**
 * @api-group EmailBounce
 */
class EmailBounce extends AbstractController
{
    
    public function actionGet()
    {
        $options = $this->options();

        $key = \XF::apiKey();

        return $this->apiResult([
            'version_id' => \XF::$versionId,
            'site_title' => $options->boardTitle,
            'base_url' => $options->boardUrl,
            'api_url' => $this->buildLink('canonical:index'),
            'key' => [
                'type' => $key->key_type,
                'user_id' => $key->key_type == 'user' ? $key->user_id : null,
                'allow_all_scopes' => $key->allow_all_scopes,
                'scopes' => $key->scopes
            ]
        ]);
    }
    
}

And here is my folder structure:

1565963369492.webp
 
Hi!

I'm looking for a way to expand the XF rest api with a couple of own endpoints, and have a question?

Where should the route.json file should placed? When adding more than an endpoint, should the additionally endpoints be added to the same json route file and controller or...?

Thanks..!
 
I managed to find the answer myself (I have learned that the route json is generated automatically when creating the route in the admin panel->development->routes).

I managed to get your sample to work, and now I can call this new endpoint and debug it with my ide. But I still have a question.

Which would be the best practice to group the endpoints of my addon? Would it be correct to add the name of my company and addon as prefix to all endpoints that I create?

And the second question. By default in your sample, the rest call is routed to the actionGet in the EmailBounce class. Is there a way to route the request to another php function in that class, and not the actionGet?

Thank you in advance!
 
Top Bottom