craftcms / element-api

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

get element by URI #107

Closed oddnavy closed 4 years ago

oddnavy commented 5 years ago

It is possible to define an element-api endpoint which can retrieve an element by URI and handle nested URIs.

For example, we have an entry with the URI of /about/our-team and would like to make an element-api query to api/pages/about/our-team or a variation of that. api/products/<uri> doesn't seem to work (I'm assuming the regex doesn't match the nested path).

brandonkelly commented 4 years ago

Yes, you can do this:

'api/<uri:.*>' => function(string $uri) {
    return [
        'elementType' => craft\elements\Entry::class,
        'criteria' => [
            'uri' => $uri,
        ],
        'one' => true,
    ];
},