<p>I had sometime to review the documentation in more depth;
<a href="https://github.com/craftcms/element-api/tree/v1#transformer" rel="nofollow noreferrer">https://github.com/craftcms/element-api/tree/v1#transformer</a></p>

<p>An abbreviated example below. Note, I include all transformers in a <code>/transformers</code> directory within <code>/config</code>.</p>

<p><em>file - craft/config/elementapi.php</em>
    

<pre><code>  return [
    'endpoints' =&gt; [
      // Home
      //
      'api/home' =&gt; function() {
        require craft()-&gt;path-&gt;getConfigPath().'transformers/home.php';
        return [
          'elementType' =&gt; ElementType::Entry,
          'criteria' =&gt; [
            'section' =&gt; 'homepage'
          ],
          'first' =&gt; true,
          'cache' =&gt; 'PT1M',
          'transformer' =&gt; new homeAPI(),
        ];
      },

      // About
      //
      'api/about' =&gt; function() {
        require craft()-&gt;path-&gt;getConfigPath().'transformers/about.php';
        return [
          'elementType' =&gt; ElementType::Entry,
          'criteria' =&gt; [
            'section' =&gt; 'about'
          ],
          'first' =&gt; true,
          'transformer' =&gt; new aboutAPI(),
        ];
      },

      // Remaining Endpoints...
      // 
    ]
  ];
?&gt;
</code></pre>

<p><em>file - craft/config/transformers/home.php</em></p>

<pre><code>&lt;?php
  namespace Craft;
  require craft()-&gt;path-&gt;getPluginsPath().'elementapi/vendor/autoload.php';
  use League\Fractal\TransformerAbstract;

  class homeAPI extends TransformerAbstract {
    public function transform(EntryModel $entry) {

      $bodyBlocks = [];
      foreach ($entry-&gt;homepageContent as $block) {
        switch ($block-&gt;type-&gt;handle) {
          case 'gallery':

            $bodyBlocks[] = [
              'gallery' =&gt; [
                'thumbnails' =&gt; $thumbnails
              ]
            ];
          break;

          // Remaining Fields...
          //
        }
      }

      return [
        'id' =&gt; (integer) $entry-&gt;id,
        'title' =&gt; $entry-&gt;title,
        'content' =&gt; $bodyBlocks,
      ];
    }
  }
</code></pre>

CC BY-SA 3.0
2017-12-19T20:52:35.373Z
2017-12-19T20:52:35.373Z
jondotblack
<p>I'm working on a <a href="https://vuejs.org/" rel="nofollow noreferrer">Vue</a> SPA (single page application) for a client and my <a href="https://github.com/craftcms/element-api" rel="nofollow noreferrer">ElementAPI</a> config file has grown to be quite large.  To make it easier to maintain I was looking for suggestions of how to have each endpoint in its own file.</p>

<p>Any feedback is appreciated!</p>

<plugin-elementapi>
Break-up Element API Config into partials
# Break-up Element API Config into partials

(Asked by: username_0 on 2017-12-07)

I'm working on a [Vue](https://vuejs.org/) SPA (single page application) for a client and my [ElementAPI](https://github.com/craftcms/element-api) config file has grown to be quite large. To make it easier to maintain I was looking for suggestions of how to have each endpoint in its own file.

Any feedback is appreciated!

**Tags:** plugin-elementapi

---

## 1 answers

### Answer by username_0 on 2017-12-19. Score: 1

I had sometime to review the documentation in more depth;
https://github.com/craftcms/element-api/tree/v1#transformer
An abbreviated example below. Note, I include all transformers in a /transformers directory within /config.
file - craft/config/elementapi.php
  return [
    'endpoints' => [
      // Home
      //
      'api/home' => function() {
        require craft()->path->getConfigPath().'transformers/home.php';
        return [
          'elementType' => ElementType::Entry,
          'criteria' => [
            'section' => 'homepage'
          ],
          'first' => true,
          'cache' => 'PT1M',
          'transformer' => new homeAPI(),
        ];
      },

      // About
      //
      'api/about' => function() {
        require craft()->path->getConfigPath().'transformers/about.php';
        return [
          'elementType' => ElementType::Entry,
          'criteria' => [
            'section' => 'about'
          ],
          'first' => true,
          'transformer' => new aboutAPI(),
        ];
      },

      // Remaining Endpoints...
      // 
    ]
  ];
?>

file - craft/config/transformers/home.php
path->getPluginsPath().'elementapi/vendor/autoload.php';
  use League\Fractal\TransformerAbstract;

  class homeAPI extends TransformerAbstract {
    public function transform(EntryModel $entry) {

      $bodyBlocks = [];
      foreach ($entry->homepageContent as $block) {
        switch ($block->type->handle) {
          case 'gallery':

            $bodyBlocks[] = [
              'gallery' => [
                'thumbnails' => $thumbnails
              ]
            ];
          break;

          // Remaining Fields...
          //
        }
      }

      return [
        'id' => (integer) $entry->id,
        'title' => $entry->title,
        'content' => $bodyBlocks,
      ];
    }
  }


---

