2

My element API endpoint throws a 404 when I'm logged out of the CP, but works fine when I'm logged in.

When logged out it throws a regular 404 with stack trace exactly as any other non-existing url, when logged in it spits out JSON as it should be.

HTTP 404 – Not Found – yii\web\NotFoundHttpException
Template not found: news.json

Notably the front-end of the page works fine always..this just affects the element API.

I tried clearing craft caches + all cookies, still the same. Found nothing suspicious in web.log, phperrors.log or web-404.log I tried it with a very simple element-api.php and it still produces the same issue.

For example, even this element-api.php does not work:

<?php

use craft\elements\Entry;

return [
    'endpoints' => [
        'news.json' => function() {
            return [
                'elementType' => Entry::class,
                'criteria' => ['section' => 'news'],
                'transformer' => function(Entry $entry) {
                    return [
                        'title' => $entry->title,
                    ];
                },
            ];
        },
    ]
];

(copied straight from the docs. The section "news" exists in my DB and has valid entries.)

Any idea what could be the cause of this or where to further investigate?

This is on Craft 3.1.12.

4
  • Can you share your code? Commented Feb 26, 2019 at 16:08
  • @TonyDeStefano shared above, but I don't think this is the cause of the issue..
    – Udo
    Commented Feb 26, 2019 at 16:31
  • Not sure if this is anything, but your endpoint is news.json and your 404 says map.json ... Commented Feb 26, 2019 at 17:50
  • nah that was just another version of the config. Thanks though for checking!
    – Udo
    Commented Feb 26, 2019 at 18:24

1 Answer 1

2

The issue ended up being related to the yii debug toolbar failing to execute correctly. Disabling it in app.php fixed it.

1
  • Please share your code. I'm experiencing a related issue, instead I've discovered when I remove this line of code Craft::$app->getUrlManager()->getMatchedElement()['id'] in my custom module, my news.json is now visible and the issue is resolved. Commented Jan 16, 2020 at 4:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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