For example, an endpoint to retrieve all the languages available on the website

'endpoints' => [   
   'languages' => function () {

            $sites = Craft::$app->getSites()->getAllGroups();

            $languages = Craft::$app->getSites()->getSitesByGroupId($sites[array_search('Languages', $sites)]['id']);

            Craft::dd($languages); // output this in a nice json

            return $languages;

Or an endpoint to retrieve all the available sections

    'sections' => function () {

            $sections = Craft::$app->getSections()->getAllSections();

            Craft::dd($sections); // output this in a nice json
  • 2
    I can't think of a clean way to do this, it would be easier to create a custom controller for it Jun 11 '18 at 9:58
  • You're right, I've made a custom module for it and it was super simple. I think I'll use this in combination with Element API. Jun 11 '18 at 10:53
  • 2
    Would you mind adding your solution as an official answer in case it helps someone in the future, @JorgeAnzola?
    – Brad Bell
    Jun 11 '18 at 18:29
  • Yes! I will do that. Jun 12 '18 at 15:23

Indeed as @robin pointed out, I couldn't figure out a nice way to making custom data outputs with Element API (Not related to a ElementType, like available Sites/Languages or available sections). What I did was:

I created a custom module, in this module I was able to register routes like:

            function (RegisterUrlRulesEvent $event) {

                $event->rules['api/languages'] = '/endpoints/endpoints/languages';

And in the action of the module. (Still is a rough version, also needs to handle possible errors and that kind of stuff)

public function actionLanguages()
    $sites = Craft::$app->getSites()->getAllGroups();

    $languages = Craft::$app->getSites()->getSitesByGroupId($sites[array_search('Languages', $sites)]['id']);

    return $this->asJson($this->formatApiResponse($languages));

I don't know if it's the nicest way (Still open to suggestions) but this gave me the freedom I needed to enhance my Element API end-points.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.