6

I've created routes shown below:

Router::connect('/:api/:controller/:action/*', array(), array('api'=>'api'));
Router::connect('/:api/:controller', array('action' => 'index'), array('api'=>'api'));
Router::connect('/:api/', array('controller' => 'index', 'action' => 'index'), array('api'=>'api'));

Basically, I want all requests made through a particular endpoint to respond in JSON. In the case above all requests made with the api prefix. For example:

http://localhost/api/products

Should return a JSON response instead of an HTML. Note that it should work that way even without the .json extension being defined.

1

1 Answer 1

9

So I am guessing in your controller you check if the api prefix was set and if so you serialize the data you give back to the view? if so then just add:

$this->RequestHandler->renderAs($this, 'json');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.