Monday, September 15, 2014

Re: Stopping a REST request in the beforeFilter() of the AppController.php

Just throw an exception:

throw new BadRequestException('Missing important headers');

On Monday, September 15, 2014 2:02:50 AM UTC+2, Geoffrey Toogood wrote:
Hey There,

So I am building an API with the built in REST functionality in CakePHP  but I am having a problem with it.

I am trying to halt a REST request after it has already been primed by the default mapResources route feature?  In the app/Config/routes.php I have the following route for my customers controller / model..

Router::mapResources('customers');

I want to add some code to the beforeFilter() of the AppController which, under certain conditions, should stop the current REST request and print a different response. 

Say I do a GET request to the customers controller...

http://localhost/cakephp/customers.json

This returns what I would expect, my customers list in JSON format. 

However I want certain HTTP_X_ headers to be present in the request before I allow it to complete so I have tried using the set method to create the response I want and tried to send() the response like so..

$this->set(array(
'status' => false,
'message' => 'Some error message',
'errors' => array('this went wrong', 'that went wrong'),
'_serialize' => array('status', 'message', 'errors')
));

$this->response->send();
$this->_stop();

In this instance what occurs is that the customers.json response completes. I want to halt the request at this point and return my error response.

Does anyone know a good way to do this?

Thanks,
Geoff T

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: