Flowing on from REST API in joomla 3.4 with SLIM Framework PART 1 it's time to to use the real power of Slim Framework. In this second part of tutorial series , we will use SLIM Framework in Joomla way of style to create powerful API.
As we learned in Part 1 of this article, how to load SLIM Framework in our joomla 3.4 project and how to instantiate SLIM framework, directory structure explained in part 1 of this tutorial, in this part we will create index.php file flowing our directory structure.
Let's start
create index.php in /your_project/api/v1/index.php . and paste bellow code, hey don't get confused by seeing this messy line of code , don't worry i will explain each line.
<?php
define('_JEXEC', 1);
define('_API', 1);
define('JPATH_BASE', dirname(dirname(dirname(__FILE__))));
// Include the Joomla framework
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
$application = JFactory::getApplication('site');
$application->initialise();
$app = new \Slim\Slim(array(
'mode' => 'development'
));
$app->_db = JFactory::getDbo();
$app->_input = JFactory::getApplication()->input;
require_once JPATH_BASE . '/api/JsonApiView.php';
require_once JPATH_BASE . '/api/JsonApiMiddleware.php';
$app->view(new \JsonApiView());
$app->add(new \JsonApiMiddleware());
//route
// Main entry
$app->get('/', function () use ($app)
{
$user = JFactory::getUser();
$name = !$user->guest ? $user->name : 'guest';
$app->render(
200, array(
'msg' => 'Welcome' . ' ' . $name,
)
);
}
);
// Content
$app->map('/content/', function () use ($app)
{
$query = $app->_db->getQuery(true);
$query->select('*')
->from($app->_db->quoteName('#__content'))
->where($app->_db->quoteName('state') . ' = ' . $app->_db->quote('1'));
$app->_db->setQuery($query);
$app->render(200, array(
'msg' => $app->_db->loadObjectList(),
)
);
}
)->via('GET');
$app->map('/content/:id', function ($id) use ($app)
{
$query = $app->_db->getQuery(true);
$query->select('*')
->from($app->_db->quoteName('#__content'))
->where('id = ' . $app->_db->quote($id)
. ' AND ' . $app->_db->quoteName('state') . ' = ' . $app->_db->quote('1')
);
$app->_db->setQuery($query);
$app->render(200, array(
'msg' => $app->_db->loadObject(),
)
);
}
)->via('GET');
$app->map('/content/', function () use ($app)
{
$user = JFactory::getUser();
if (count($user->getAuthorisedCategories('com_content', 'core.create')) > 0)
{
$row = new stdClass();
$row->title = $app->_input->get('title');
$row->introtext = $app->_input->get('introtext');
$row->created_by = $user->id;
$row->state = '1';
$app->_db->insertObject('#__content', $row);
$app->render(200, array(
'msg' => $row->title . ' created!',
)
);
}
$app->render(403, array(
'msg' => 'Not authorized',
)
);
}
)->via('POST');
$app->map('/content/:id', function ($id) use ($app)
{
$user = JFactory::getUser();
if ($user->authorise('core.edit', 'com_content.article.' . $id) || $user->authorise('core.edit.own', 'com_content.article.' . $id))
{
$row = new stdClass();
$row->id = $id;
$row->title = $app->_input->get('title');
$row->introtext = $app->_input->get('introtext');
$row->state = '1';
$result = $app->_db->updateObject('#__content', $row, 'id');
$app->render(200, array(
'msg' => $result,
)
);
}
$app->render(403, array(
'msg' => 'Not authorized',
)
);
}
)->via('PUT');
$app->map('/content/:id', function ($id) use ($app)
{
$query = $app->_db->getQuery(true);
$query->delete($app->_db->quoteName('#__content'))
->where('id = ' . $app->_db->quote($id));
$app->_db->setQuery($query);
$app->render(200, array(
'msg' => $app->_db->query(),
)
);
}
)->via('DELETE');
$app->run();
let me explain line Number 7 to 8. here we are calling Joomla define.php and framework.php. framework.php is responsible to atutoload Slim library.
$application = JFactory::getApplication('site');
$application->initialise();
here we are getting some joomla site information that can be helpful in generating joomla related information.
$app = new \Slim\Slim(array(
'mode' => 'development'
));
here we are creating $app object of slim by assigning Development mode..
$app->_db = JFactory::getDbo();
$app->_input = JFactory::getApplication()->input;
this way we are connecting to joomla databse with slim.
require_once JPATH_BASE . '/api/JsonApiView.php';
require_once JPATH_BASE . '/api/JsonApiMiddleware.php';
$app->view(new \JsonApiView());
$app->add(new \JsonApiMiddleware());
here we require JsonApiView.php JsonApiMiddleware.php that we have created in /your_project/api/JsonApiMiddleware.php and in /your_project/api/JsonApiView.php. this class we will learn letter, before digging into it lets create route for our API.
The Slim Framework helps you map resource URIs to callback functions for specific HTTP request methods (e.g. GET, POST, PUT, DELETE, OPTIONS or HEAD). A Slim application will invoke the first route that matches the current HTTP requests URI and method.
//route
// Main entry
$app->get('/', function () use ($app)
{
$user = JFactory::getUser();
$name = !$user->guest ? $user->name : 'guest';
$app->render(
200, array(
'msg' => 'Welcome' . ' ' . $name,
)
);
}
);
when you hit your URL http://localhost/your_project/api/v1/
you will get this output
{"msg":"Welcome guest","error":false,"status":200}
so we have learned basic routing and getting joomla login name if user is logged in otherwise we will get Welcome guest.
now suppose we want to create route for content,
// Content
$app->map('/content/', function () use ($app)
{
$query = $app->_db->getQuery(true);
$query->select('*')
->from($app->_db->quoteName('#__content'))
->where($app->_db->quoteName('state') . ' = ' . $app->_db->quote('1'));
$app->_db->setQuery($query);
$app->render(200, array(
'msg' => $app->_db->loadObjectList(),
)
);
}
)->via('GET');
hit http://localhost/your_project/api/v1/index.php/content/ and you will get your Joomla content in JSON format.
and so on.... you can create whatever route you wish for your API..
you can find more about routing in Slim Framework site .
You can find attachment for demo. This was just a simple example. I hope Ive convinced you to start writing Joomla API with the help of Slim Framework. If you have any query feel free to ask in comments.
The advance feature will be provided in the next part of the series, when well also take a look at one of the token, verification, paginated data for the API and most useful feature of SLIM Framework.
2 Comment(s)