We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

REST API file extension

I want to return responses depending on the file extension of the request made to my Rest API:

1 case - No extension like /api/robots/1 I 'd like to return response with XML

2 case - /api/case/1.json I'd like to return response in JSON

The same way i would like to return response in .txt and .html

What is the way phalcon would deal with it?

Could not find such topic, please advise



34.6k
Accepted
answer

You can create routes where a parameter is the extension:

$router->add(
    "/documentation/{chapter}/{name}\.{type:[a-z]+}",
    array(
        "controller" => "documentation",
        "action"     => "show"
    )
);

https://docs.phalcon.io/en/latest/reference/routing.html#parameters-with-names