don't worry be api with slim framework and joomla
DESCRIPTION
Jab14 exemple of integration of Joomla and slim Framework http://jandbeyond.org/program/sessions/dont-worry-be-api-best-practices-and-implementatio.html In the context of a website or web application, an application-programming interface or API allows software developers to interact with and build upon the data and/or services delivered by your website. Thus, APIs provide limitless flexibility, reach and innovative potential for any website, service or application. During the last few months, Pierre-André has performed extensive research and development to discover effective ways of adding an API to a Joomla website. In this interactive session, he will share his findings. Attendees can expect to learn, why you need an API for your web site, the pros and cons of various API solutions, and why a RESTful API is a good fit for Joomla. Best practices for implementing a well-documented, pragmatic, and RESTful API will be discussed, along with useful tools and sample code for using the com_api component as well as Slim Framework.TRANSCRIPT
Don't worry be “API” / JaB 2014
Alway’s be watching!
Fran
kfur
t ©Fl
ickr
- Je
ns M
ayer
How are you ?
→ Have an API or plan one ?→ Developer / designer / integrator ?→ How many APIs have you already use
today ?
Pierre-André Vullioud
Watchful.li CTO
Joomla enthusiast
Owner of inetis.ch
#pavullioud
APIs in 2014
source : www.programmableweb.com/api-research
Amazon
→ All teams will expose their data and functionality through API.
→ There will be no other form of interprocess communication [..]
→ Anyone who doesn't do this will be fired.Amazon policy set by Jeff Bezos in 2003
Open your system
→ Internal use→ Apps→ Clients→ Partners
Data
API
Functionality
app
partnersservices
REST
Everything is a resource
verbs : GET, PUT, POST, DELETE, PATCH
GET /sessions
Relations
GET /speakers/12/sessions
Filtering, sorting,
GET /sessions?date=20140601
GET /sessions?sort=+date,+name
Limit fields, callback
GET /sessions?fields=date,name,id
GET /sessions?callback=myFunction
API interfaces for Joomla
J!1.5 : XML-RPC
J!2.5 : com_api
J!3.2 : com_ajax and com_api
External solution : Slim Framework
com_api
for J! 2.5 & 3.x
a component by Techjoomla
extended by pluginsExample of call : index.php?option=com_api&app=jab&format=raw&resource=sessions&id=14&api_key=123456qwert12http://techjoomla.com/rest-api-for-joomla
com_api
Pro
inside Joomla
compatible 2.5/3.x
Con
code based on J!1.5
URL not clean or fight with router.php
com_ajax
Since J! 3.2 in the core
Extended by plugins & modules
First use for intern ajax callExample of call : index.php?option=com_ajax&plugin=session&format=jsonhttp://docs.joomla.org/Using_Joomla_Ajax_Interface
com_ajax
Pro
core of Joomla compatible J!1.5/2.5/3.x
Con
URLs not clean
Slim Framework
PHP micro framework
REST URLs based
LightExample of call :
/api/v1/sessions/12slimframework.com
Slim Framework
Pro
Clean URLs
RESTFull
Cons
Not inside J!
Need to learn
Files
api/ Slim
v1/
{..}
.htaccess
index.php
End point = https://mysite.com/api/v1/session
Weeding J!Slim
require_once ( JPATH_BASE . '/includes/defines.php' );
require_once ( JPATH_BASE . '/includes/framework.php' );
$application = & JFactory::getApplication('site');
$application->initialise();
require '../Slim/Slim.php';
Add Middleware
$app = new \Slim\Slim
$app->_db = JFactory::getDbo();
$app->view(new \JsonApiView());
$app->add(new \JsonApiMiddleware());
Define route and execute
$app->get('/', function() use ($app) {
$app->render(200, array(
'msg' => 'You reach the JAB API V1' )); });
$app->map('/sessions/', function() use ($app) { ...})->via('GET');$app->map('/sessions/:id', function($id) use ($app) { ...})->via('GET');
$app->run();
Demo
Documentation
API is only as good as its documentation
Swagger
describing, producing, consuming, and visualizing RESTful web services
Specification: https://github.com/wordnik/swagger-spec
Swagger™ Demo: http://petstore.swagger.wordnik.com/
PHP comments
* @SWG\Api(
* path="/logs/metadata",
* @SWG\Operation(
* method="GET",
* summary="Get the list of fields",
* notes="Returns a list of fields",
* type="Logs",
* nickname="getFieldsLogs" (…)
Parse the code
Code Parser JSON output
Use it
Generate online documentation
http://petstore.swagger.wordnik.com/
Generate clients in different languages
https://github.com/wordnik/swagger-codegen
Share you api : http://apicommons.org/apis.html
Other tools
Consoles :
App for chrome
Online console
API proxy :http://apigee.com
http://www.3scale.net
http://www.mashery.com/
Questions ?
Ressources
Github : https://github.com/pvullioud/joomla-slimframework