Practical REST API
-
Upload
aki-bjoerklund -
Category
Technology
-
view
319 -
download
1
Transcript of Practical REST API
![Page 1: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/1.jpg)
Practical REST APIWhy admin-ajax.php should be avoided and how
Aki Björklund CTO,H1
@akibjorklund
![Page 2: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/2.jpg)
Communication between JavaScript and PHP is done with Ajax
![Page 3: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/3.jpg)
Historically that is done in WordPress by calling
/wp-admin/admin-ajax.php
![Page 4: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/4.jpg)
Why admin-ajax.php is bad?
![Page 5: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/5.jpg)
1. Inefficient
![Page 6: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/6.jpg)
2. Not cached
![Page 7: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/7.jpg)
3. No infrastructure like authentication, data
validation, …
![Page 8: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/8.jpg)
4.Ad hoc: messy
![Page 9: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/9.jpg)
WordPress REST API in core from 4.4
![Page 10: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/10.jpg)
There are no built-in endpoints yet
![Page 11: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/11.jpg)
Unless you install the REST API plugin
![Page 12: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/12.jpg)
In many cases no need to write any PHP to do a lot.
![Page 13: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/13.jpg)
But what can you do just with what the core already has?
![Page 14: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/14.jpg)
Example: Comment Reactions
![Page 15: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/15.jpg)
![Page 16: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/16.jpg)
Converting admin-ajax.php to REST API
![Page 17: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/17.jpg)
Before
![Page 18: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/18.jpg)
![Page 19: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/19.jpg)
![Page 20: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/20.jpg)
![Page 21: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/21.jpg)
![Page 22: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/22.jpg)
After
![Page 23: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/23.jpg)
![Page 24: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/24.jpg)
![Page 25: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/25.jpg)
![Page 26: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/26.jpg)
![Page 27: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/27.jpg)
Best practice is to implement what you just saw as a REST Controller. Currently available only with the REST API plugin.
![Page 28: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/28.jpg)
Summary
![Page 29: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/29.jpg)
Use the REST API instead of admin-ajax.php
![Page 30: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/30.jpg)
It’s faster and cleaner
![Page 31: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/31.jpg)
It’s also both easy to implement and migrate to
![Page 32: Practical REST API](https://reader030.fdocuments.net/reader030/viewer/2022021502/58f1ec8b1a28ab3f1d8b4599/html5/thumbnails/32.jpg)
.
Aki Björklund CTO,H1
@akibjorklund