Sunday, February 13, 2022

[FIXED] How to use CakePHP exclusively for the API?

Issue

I have a API made with CakePHP and I don't want to use the views to display something on the website, for example I'd like to be able to use api.mydomain.com/users in CURL, but if I try to go to the URL in my browser I want it to redirect to api.mydomain.com which will be my API documentation. I want this to be for every controller.

I don't know if my question is clear, but I only want my site to serve JSON responses and nothing else. If there's a better solution to my question, I'd like to hear it too.

Thanks for helping.


Solution

You need to use JSON and XML views. In this way, you'll render a JSON response.

I don't think you should redirect to the api.mydomain.com, it's pretty common to access API through a browser, but if you really want to do it you have to take a look at $_SERVER['HTTP_USER_AGENT'] variable



Answered By - SamHecquet

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.