craftcms / element-api

Create a JSON API/Feed for your elements in Craft.
MIT License
498 stars 56 forks source link

404 Not Found — JSON URL / UrlHelper #173

Open a-rbsn opened 1 year ago

a-rbsn commented 1 year ago

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

brianjhanson commented 1 year ago

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?