element-api icon indicating copy to clipboard operation
element-api copied to clipboard

404 Not Found — JSON URL / UrlHelper

Open a-rbsn opened this issue 3 years ago • 1 comments

Description

I'm using the UrlHelper to generate JSON URLs for entries but the generated URL doesn't seem to work.

Steps to reproduce

        'projects.json' => function () {
            Craft::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', '*');
            return [
                'elementType' => Entry::class,
                'criteria' => [
                    'section' => 'projects',
                ],
                'paginate' => false,
                'transformer' => function (Entry $entry) {

                    return [
                        'id' => $entry->id,
                        'slug' => $entry->slug,
                        'jsonUrl' => UrlHelper::url("project/{$entry->slug}.json"),
                    ];
                },
            ];
        },
        'project/<entrySlug>.json' => function ($entrySlug) {
            Craft::$app->getResponse()->getHeaders()->set('Access-Control-Allow-Origin', '*');
            return [
                'elementType' => Entry::class,
                'criteria' => [
                    'section' => 'projects',
                    'slug' => $entrySlug,
                ],
                'one' => true,
                'transformer' => function (Entry $entry) {
                    return [
                        'id' => $entry->id,
                        'title' => $entry->title,
                        'slug' => $entry->slug,
                    ];
                },
            ];
        },

Additional info

  • Craft version: 4.3.4
  • PHP version: 8.2
  • Database driver & version: MySQL
  • Plugins & versions: ElementAPI 3.0.1.1

a-rbsn avatar Dec 05 '22 13:12 a-rbsn

Can you add any detail about how it doesn't work and your particular config? Are the correct URLs being generated? Or are you getting an unexpected URL?

brianjhanson avatar Dec 09 '22 16:12 brianjhanson