» CakePHP REST Plugin presentation
At our company we have a lot of uses for a solid API. We can use it to distribute config files, have servers report in, let customers edit DNS records using their own interface, etc.
Now that I'm converting all of our legacy code to a big CakePHP application, the API needed a revisit as well. I chose to use REST as a standard, read about everything related to Cake & REST, and started hacking on a reusable plugin. The idea is that you can drop it in any application and unlock existing functionality to REST with minimal changes to your code.
It is still a work in progress, but as the first Dutch CakePHP event was held yesterday and I was asked to present something I thought this particular plugin might be of interest to the community. Here are the slides:
View on slideshare
And here's the source & documentation on github
I would love some feedback to help it make better. My todos can be found in the slides as well, to give you an idea where I'm heading with this.
More info on the Dutch CakePHP event
- @jsentel's writeup on the talks
- Singel 146's followup (the location in Amsterdam)
- @charli3's initial post
- #cakephpnl on twitter
- LinkedIn group: CakePHP NL
You probably shouldn't follow me
Like this Article?
| I'd appreciate it if you leave a comment, spread the word, or consider a small donation |
RelatedArticles like this one» Prepare for PHP 5.3 |
tags: cakephp, programming, rest, php, api
category: Programming - PHP - CakePHP
read: 17,356 times
tagcloud
#44. Kevin on 15 December 2011
#43. Gary on 09 December 2011
#42. Chris on 23 November 2011
That will make it work on function index() and json/xml encode the $prospects array.
#41. Chris on 23 November 2011
My controller code is
My htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
# Adds AUTH support to Rest Plugin:
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization},last]
</IfModule>
And my routes.php is
#40. chiacy on 14 November 2011
I tried the authentication, first, I do not know how apikey is generated, but that's not really a matter.
What matters me is that once a client logged in with correct KEYWORD, username and password, and when trying to access subsequently the same site from the same client, as long as KEYWORD is correct, even without username and password, I can still access to the data.
... [more]
I wonder if this is due to cookies, but I have disabled cookie from my cakephp component to eliminate the possibility, no good result.
Anyone out the can help pointing out I have done wrong?
added the following to component settings:
'onlyActiveWithAuth'=>true,
'catchredir' => false,
both either true or false, no effect.
before filter function of the controller
if (!$this->Auth->user()) {
// Try to login user via REST
if ($this->Rest->isActive()) {
$credentials=$this->Rest->credentials();
$this->Auth->autoRedirect = false;
$data = array(
$this->Auth->userModel => array(
'username' => $credentials['username'],
'password' => $credentials['password'],
),
);
$data = $this->Auth->hashPasswords($data);
if (!$this->Auth->login($data)) {
$msg = sprintf('Unable to log you in with the supplied credentials. ');
return $this->Rest->abort(array('status' => '403', 'error' => $msg));
}
}
}
Thanks
#39. chiacy on 14 November 2011
changes made here:
'actions'=>array(
... [more] 'index' => array(
'extract' => array('users'),
),
),
instead of just
'index' => array(
'extract' => array('users'),
),
Thank you!!!
#38. linkingarts on 14 November 2011
I have updated my controller to use actions => array but this only gives me {"data":{"mythings":[null]}...}
Any help most appreciated. Great plugin, thank you for opening it up (I have used it before on other sites - not sure why is not working now!).
#37. chiacy on 13 November 2011
This is THE plugin I am dreaming of! It allows me to systematically convert existing cake app to REST based.
To test, I am using one of my cake app and play around with User Controller.
... [more]
I have follow every step you mentioned in your read me etc, and read thru all comments, but I can't seems to get the right.
Currently, no authentication is implemented, just to make it as simple as possible until I get the desired result.
Below is the problem
My data is always empty, as shown below
{"data":[],"meta":{"status":"ok","feedback":[],"request":{"http_host":"127.0.0.1","http_user_agent":"Rested 2.0.1 (Macintosh; Mac OS X 10.7.2; en_MY)","server_addr":"127.0.0.1","remote_addr":"127.0.0.1","server_protocol":"HTTP/1.1","request_method":"GET","request_uri":"/ap5/users/index.json","request_time":1321202674},"credentials":{"class":null,"apikey":null,"username":null},"time_epoch":"1321202674","time_local":"Mon, 14 Nov 2011 00:44:34 +0800","version":"0.3"}}
Appreciate if you can point me to the solution.
component settings as below:
'Rest.Rest'=>array(
'catchredir' => false, // Recommended unless you implement something yourself
//'debug' => 2,
'view' => array(
'extract' => array('user'),
),
'index' => array(
'extract' => array('users'),
),
'ratelimit'=>false,
'log'=>null,
'extensions' => array('xml', 'json', 'csv'),
),
Users Controller index function
function index() {
$this->User->recursive = 0;
$users=$this->User->find('all');
//$this->set('users', $this->paginate());
$this->set(compact('users'));
debug($users);
}
thank you in advance
#36. albert liga on 09 November 2011
can you help me how to create REST plugin in cake step by step???
#35. Scott McDonnell on 23 August 2011
1. In Authorization the example does not set $credentials
2. The 2 examples in Usage are not in the new format ie. contained in an 'action' array. They should read
and
Just makes it a lot easier to get going quickly!
#34. Peter DeMartini on 23 August 2011
#33. Carlos on 08 June 2011
http://book.cakephp.org/view/1238/REST
As far as I can tell:
... [more] - authentication
- json support
- rate limiting
- logging
Did I miss anything?
#32. Diego Massanti on 27 April 2011
But like some other people asked here, i would like to see an example of how to deal with checking if rest is enabled inside my error handler.
Right now i have a custom subclass of errorHandler for my custom errors, and unless i send a reference to the calling controller to my custom error function, i can not figure out how to get a reference to the Rest plugin, could you post some actual code example on how you dealt with this ?
... [more] Thanks.
Diego.
#31. Kevin on 17 April 2011
@ Florus: That's not really enough information to go on, sorry.
#30. Florus on 12 April 2011
With CakePHP 1.3.8. running (fresh install) I tried to get your Rest plugin into action. But I'm getting errors when I try to access .json or .xml via browser.
I have the mapResources set and the parseExtensions etc, like your install notes says.
The parsing via the browser seems to handle the content-type well, like for xml and json, but the content itself are errors only..
... [more]
Thanks, Florus
#29. Freddy on 05 April 2011
This line are not important because the problem the function headers() want to fixe isn't a problem. The header are good when your debug level is 0 (both REST and Core).
#28. Freddy on 04 April 2011
When I use the REST component, for ADD action $this->data is empty (it works fine when i don't use this component).
ps : sorry for my english, i'm french
#27. Juan on 28 March 2011
#26. Kevin on 04 March 2011
#25. Gary on 27 February 2011
Thanks!
#24. Kevin on 23 February 2011
This behavior is configurable but I changed the default:
https://github.com/kvz/cakephp-rest-plugin/commit/c05c59b506054e1e06113b2562414607e962e344
... [more] Let me know if that helps!
#23. luqash on 22 February 2011
using CakePHP 1.3.7
#22. Marco on 04 February 2011
But I have same problem Cazza and Gary have, do we miss something?
Do you guys got this problem and managed to resolve?
Thanks in advance.
#21. Carlos on 31 January 2011
Can you provide a code sample for this? Slide #14 touches on this but I can't figure out to code it.
My problem is that code errors (not api call errors) get output in XML/JSON so debugging is messy.
... [more]
Ideally code errors would cause rest output to deactivate and output the errors in regular HTML (instead of invalid xml/json).
Thanks!
#20. Babar on 31 December 2010
i am getting this Fatal error: Class 'XmlView' not found in D:xampphtdocsltfcakelibscontrollercontroller.php on line 775, and i am unable to debug it.
#19. Gary on 31 December 2010
This will significant simplify my development, if I can get it to work.
Unfortunately, I'm getting the same results as Cazze. Whenver I try to access a .json or .xml, I get a "Missing View" error saying I need to create xml/index.ctp and json/index.ctp files.
... [more]
I'm using Cake 1.3.6. Is there an incompatibility with this version?
#18. Francisco on 10 December 2010
#17. Cazze on 06 December 2010
i tried your plugin, but when i go to .xml or .json, i always got an error view not found.
Do i have to create views for xml and json?
#16. Kevin on 31 October 2010
#15. Chris on 27 October 2010
Great plugin!
Just wanted to say I was only able to get it working after creating the rest_logs table (using the sql in config/schema/rest_logs.sql) - if I didn't do that, I got a Page not found error message when attempting to get the .json version.
... [more]
Might be helpful for anyone else experiencing the same issue.
Chris
#14. Kevin on 14 October 2010
#13. Kevin on 08 September 2010
But if you feel like giving it a spin.. I'd be happy to learn your findings / accept your patches.
#12. Paul Winkeler on 16 August 2010
#11. Kevin on 29 June 2010
Furthermore, you may find the clien-side of this API interesting that I developed for my company:
http://github.com/true/true-api
#10. steve on 22 June 2010
[0] => Content-Disposition: attachment; filename="DSC01264.JPG"
[1] => Content-Type: application/octet-stream
[2] => Content-Length: 64250
... [more] [3] => Authorization: OAuth realm="", oauth_requester_id="720", oauth_signature_method="HMAC-SHA1", oauth_signature="Rom3hREK8p2PzMBXdzIJb3hy%2FF0%3D", oauth_nonce="4c210a01d83d9", oauth_timestamp="1277233665", oauth_token="", oauth_consumer_key="1385adb7be6ade0a0bf7d5493fd0722404c1795eb", oauth_version="1.0", xoauth_body_signature="8ap46NO3buRxIbzAtFc5OFa6FNY%3D"
thanks,
Steve
#9. steve on 11 June 2010
I am building a REST API now, and I could use all the advice I could get.
Thanks!
Steve
#8. Kevin on 10 June 2010
as (kind of) explained on: http://github.com/kvz/cakephp-rest-plugin
#7. steve on 08 June 2010
Thank you very much for this plugin! I am a bit confused about the authentication. Does this plugin actually handle the Amazon-style authentication and return the appropriate HTTP response codes? Or, does it simply parse out the Amazon-style Header:
`Authorization: TRUEREST username=john&password=xxx&apikey=247b5a2f72df375279573f2746686daa`
... [more] into an array that can then be used effectively with some other security component?
Thanks,
Steve
#6. Kevin on 29 April 2010
Your index is fine.
If in that controller you specify:
It should show you the contents of products under 'myProducts' in json format, when you browse to /index.json
#5. gkibria on 24 April 2010
And Here is my index()
my cake_core is 1.25 thanks
#4. Kevin on 24 April 2010
No the trick is you
like you would normally with your index()
Only the restcomponent can take the viewVars you specify, maybe even convert them into another (more rest-consistent) format, and them json_encode them.
XML serialization is being worked on.
#3. gkibria on 24 April 2010
Thanks for your plugin. I have a products controller with index action working fine. When I added Rest.Rest component in my products controller I got "Error: The requested address '/products' was not found on this server." for /products
And /products.xml give me" XML Parsing Error: no element found"
... [more]
When I disable Rest.Rest component, /products are working fine. Can you tell me what's wrong?
Another thing, Do I have to place any additional code in index() action?
Many Thanks
#2. Kevin on 14 March 2010
/posts/view/1
you could add the Rest component to your PostsController and then you could do
/posts/view/1.json
... [more]
to get the same data but in json format and with additional features like authentication
#1. Tamas Jozsa on 12 March 2010
This plug-in is exactly what I'm searching for. Will try it out today and when I have time I will return to give a feedback.
One thing I do not understand so far is, how to call this from a browser. Is it like:
... [more]
www.hostname.com/rest/<modelname>s ???
And how to select whether to output to JSON or XML. As I see in the code XML is there in many places.
Thanks,
Regards,
Tamas