Add file url to API (page.php)

Iā€™m tweaking the API a bit, and I would like to return the file URL (not .txt) ā€œattachedā€ to page.

Here is what I tried:

   'fileLink' => function (Page $page){
        return $page->file();
    },

Iā€™m trying to display it here

api/pages/home/children

but for some reason it returns

ā€œmessageā€: ā€œMaximum function nesting level of ā€˜256ā€™ reached, aborting!ā€,

Is there a way to get the file at this level?

Cheers

Just for info,
a page under home called ā€œmyaudioā€ has a file attached called ā€œcoolaudio.mp3ā€.
If I add the ā€œhasFilesā€ call to the API it shows ā€œtrueā€, now I want to return the file url at the same level
(hope itā€™s clear enough haha)

Not really :wink:

What is this? a page method? Iā€™m missing the context of this bit of code.

What I think you might want to achieve is show file URLs which do not link to the media folder but keep the content folder in the URL? :thinking:

ooops, my bad haha

The page.php Iā€™m talking about is under

/config/api/models/page.php

The bit of code Iā€™m trying to add is placed under

/**

  • Page
    */
    return [
    ā€˜fieldsā€™ => [

So basically Iā€™m trying to add this new property the same way we are calling

ā€˜idā€™ => function (Page $page) {
return $page->id();
},

Iā€™m trying to show the file URLs indeed, I donā€™t really mind if itā€™s under the media folder or not, itā€™s mostly to return the media URL without having to do a second API call.

To give a bit more context, I have created a new page here

/content/1_home/3_myaudio/

and added a mp3 file with it

/content/1_home/3_myaudio/coolaudio.mp3

Iā€™m trying to display the mp3 URL when doing the API call:

http://localwebsite/api/pages/home/children

Hope itā€™s a bit clearer? :smiley:

So you are actually trying to modify Kirbyā€™s source code? Doesnā€™t sound like a great idea.

If you want to treat the audio file as a child of the home page, it should rather be a virtual subpage of this page, define in the home pageā€™s page model.

the problem is that it will still require to do a second API call which is going to make things more complicated for our purpose.

Do you know if itā€™s possible to write our own API calls without modifying the source code?

Cheers

That would be handy to add additional returned object to the default api call made by kirby!

I have a case where I would need my api call to return images datas such as the alts and captions. And the current api model by Kirby does not do it! :confused:

These fields

How can I add them to the default api response made by kirby ?