0

Alright, so a better title here may have been "Progressive Enhancement with REST in CakePHP", but at least now I'll know you didn't read the question if your answer just refers to the difference between the two ;)

I'm pretty familiar with REST and how to integrate it with CakePHP, but I'm not 100% on board with how to still maintain a conventionally functioning website. Using Router::mapResources sounds like a great idea, but this creates a problem with maintaining the "gracefully degradation" version of the site, because both POST requests to /resource/ AND GET requests for /resource/add will route to the same action (add). Clearly I'll want this action to return a JSON object if they're using the REST api, but if they're using the degraded version of the site (no JS perhaps), it should be a add form, right?

What's the best way to deal with this. Do you route your REST requests to other action names using Router::resourceMap()? Do you do that crazy hack I saw to have the /api/ prefix part of the resourceMap so you can use api_action functions? Do you have the actions handle both REST and conventional requests via checking isAjax()? If so, how do you ensure that you can rely on the browser to properly support the other two request types?

I've searched around quite a bit but haven't found anything about how to keep conventional requests available in Cake along side REST, so if anyone has any advice or experience, I'd love to hear it!

BeniRose
  • 412
  • 5
  • 13

1 Answers1

0

CakePHP uses extension routing as well, via Router::parseExtension() so;

  • /test/action will render views/test/action.ctp
  • /test/action.html also
  • /test/action.json will render views/test/json/action.ctp
  • /test/action.xml will render views/test/xml/action.ctp

If all views are designed to handle the same data as set by your controller, you'll be able to show a regular HTML form and handle the posted data the same way as you'd handle the AJAX request.

You'll probably might have to add checks if any data is posted/submitted inside the /add, /edit, /delete actions to prevent items being deleted without a form being posted (haven't tested that though, it might be that cake blocks these urls if mapresources is set for the controller)

REST in CakePHP: http://book.cakephp.org/2.0/en/development/rest.html

(Extension) Routing http://book.cakephp.org/2.0/en/development/routing.html#file-extensions

thaJeztah
  • 27,738
  • 9
  • 73
  • 92
  • Thanks for the info, I understand this part, I guess my question was what do you do in the controller, because it seemed like actions for REST API would do something different than actions for traditional requests, but you might be right, they may be using the same data and the view is what's doing something different. Thanks! – BeniRose Feb 28 '13 at 22:56