1

I've been trying to clean up my elementapi.php file, and one very important step in making it more DRY is to have reusable transformer classes.

But I cannot seem to get it to work, the following gives me a blank page.

So far in my elementapi.php file I have the following.

'api/articles' => function() {
// I have also tried to place this line at the very top of the file.            
require_once craft()->path->getConfigPath().'transformers/EntryInfoTransformer.php';

            'criteria' => [
                'section' => 'blog',
                'order' => 'postDate desc',
            ],

            'elementsPerPage' => 5,
            'elementType' => 'Entry',
            'transformer' => new EntryInfoTransformer(),
        },

and my EntryInfoTransformer.php file has

<?php

require craft()->path->getPluginsPath().'elementapi/vendor/autoload.php';

use League\Fractal\TransformerAbstract;
class EntryInfoTransformer extends TransformerAbstract{
    public function transform(EntryModel $entry) {
        return [
            'id' => $entry->id,
        ]
    },
}

I don't get any errors, nothing in either the php or craft logs, just a blank while page. This has been tested on php 5.5 & 5.6. Site runs normally otherwise.

2

You're getting a whitescreen because you've got some PHP syntax errors in both files. Craft isn't reporting them because those config files get loaded before Craft has had a chance to bootstrap. The underlying errors should be recorded in your web server's error logs depending on your default server configuration.

Try these:

elementapi.php

<?php
namespace Craft;

require_once craft()->path->getConfigPath().'transformers/EntryInfoTransformer.php';

return [
    'endpoints' => [
        'api/articles' => function() {
            return [
                'criteria'        => [
                    'section' => 'blog',
                    'order'   => 'postDate desc',
                ],
                'elementsPerPage' => 5,
                'elementType'     => 'Entry',
                'transformer'     => new EntryInfoTransformer(),
            ];
        },
    ]
];

EntryInfoTransformer.php

<?php
namespace Craft;

require craft()->path->getPluginsPath().'elementapi/vendor/autoload.php';

use League\Fractal\TransformerAbstract;

class EntryInfoTransformer extends TransformerAbstract
{
    public function transform(EntryModel $entry){
        return [
            'id' => $entry->id,
        ];
    }
}
| improve this answer | |
  • Just tried both files, nothing else but the code above in my files, and it still returns a blank page. I've checked the system level log files and they output issues for me, but not of the kind that I want and the last one to be logged was 2 days ago. dropbox.com/s/c958xysma589x5q/… – Sean O'Grady Oct 9 '15 at 20:25
  • 1
    Updated. The transformer class needs to be in the Craft namespace. – Brad Bell Oct 9 '15 at 21:04

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.