I'm new to Slim and the three layered approach I'm using below. So far I have the API, a Presentation layer (leaving out for now), a Business Logic Layer, and a Data Access Layer. The code's working, but I know it can be improved a lot. I'd really appreciate some feedback from you all.
A few things I'm questioning already:
- Do I really need to create a new BLL object for each method in the API?
- In the DAL I'm sure I shouldn't be connecting to the database in each method. What would be a better approach?
- With the Slim routes, it seems like /calendars/:id is getting redundant. Instead of always passing the ID up to the BLL then to the DAL, perhaps a single session variable in the DAL would be cleaner?
Of course the code below has been trimmed to keep it short, but hopefully you get the idea.
Slim API
<?php
require $_SERVER["DOCUMENT_ROOT"] . '/api/BLL/BLL.php';
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim(array(
'debug' => true
));
$app->log->setEnabled(true);
$app->group('/v1', function () use ($app) {
$app->get('/calendars/:id/events', 'getEvents');
$app->get('/calendars/:id/event/:eid', 'getEvent');
$app->get('/calendars/:id/users(/:type)', 'getUsers');
$app->get('/calendars/:id/categories', 'getCategories');
$app->get('/calendars/:id/locations', 'getLocations');
$app->get('/calendars/:id/holidays', 'getHolidays');
$app->post('/calendars/:id/categories', 'addCategory');
});
$app->run();
function getHolidays ($id) {
$bll = new BusinessLayer();
$result = json_encode($bll->getHolidaysBLL($id));
echo '{"holidays": ' . $result . '}';
}
function getUsers ($id, $type = '') {
$bll = new BusinessLayer();
$result = json_encode($bll->getUsersBLL($id, $type));
echo '{"user": ' . $result . '}';
}
function getEvents ($id) {
$bll = new BusinessLayer();
$result = json_encode($bll->getEventsBLL($id));
echo $result;
}
function getEvent ($id, $eid) {
$bll = new BusinessLayer();
$result = json_encode($bll->getEventBLL($id, $eid));
echo '{"event": '$result . "}";
}
function addCategory ($id) {
$request = \Slim\Slim::getInstance()->request();
$category = json_decode($request->getBody());
$bll = new BusinessLayer();
$result = json_encode($bll->addCategoryBLL($category->cat_name, $category->cat_color, $id));
echo $result;
}
Business Logic Layer
<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/api/DAL/DAL.php";
class BusinessLayer
{
var $dal;
function __construct() {
$this->dal = new DataLayer();
}
public function getEventsBLL ($id)
{
//echo "From BLL getEventsBLL: " . $id . " " . $type; exit;
$ret = $this->dal->getEventsDAL($id);
return $ret;
}
public function getEventBLL ($id, $eid)
{
//echo "From BLL getEventBLL: " . $id ; exit;
$ret = $this->dal->getEventDAL($id, $eid);
return $ret;
}
public function getHolidaysBLL ($id) {
//echo "From getHolidaysBLL: " . $id; exit;
$ret = $this->dal->getHolidaysDAL($id, $type);
return $ret;
}
public function getUsersBLL ($id, $type)
{
//echo "From getUsersBLL: " . $id . " " . $type; exit;
$ret = $this->dal->getUsersDAL($id, $type);
return $ret;
}
public function getCategoriesBLL ($id)
{
//echo "From BLL getCategoriesBLL: " . $id . " " . $type; exit;
$ret = $this->dal->getCategoriesDAL($id);
return $ret;
}
Data Access Layer
<?php
class DataLayer
{
public function connect()
{
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "database_here";
$db = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
public function getUsersDAL ($id, $type)
{
$sql = "SELECT * FROM users
WHERE calendar_id = :id";
if ($type == "admins") {
$sql .= " AND isadmin = :isadmin";
}
if ($type == "managers") {
$sql .= " AND allowcats = :allowcats";
}
$sql .= " ORDER BY id";
try {
if (!empty($type)) {
$value = 1;
}
$db = $this->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
// need to loop through these instead of hardcoding
if ($type == "admins") {
$stmt->bindParam("isadmin", $value);
}
if ($type == "managers") {
$stmt->bindParam("allowcats", $value);
}
$stmt->execute();
$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
$db = null;
return $ret;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
public function getCategoriesDAL ($id)
{
$sql = "SELECT * FROM categories
WHERE calendar_id = :id
ORDER BY cat_name ASC";
$db = $this->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$ret = $stmt->fetchAll(PDO::FETCH_ASSOC);
$db = null;
return $ret;
}
public function addCategoryDAL ($name, $color, $id)
{
$sql = "INSERT INTO categories (
cat_name,
catcolor,
calendar_id
)
VALUES (:cat_name, :catcolor, :calendar_id)";
//return $sql;
try {
$db = $this->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam("cat_name", $name);
$stmt->bindParam("catcolor", $color);
$stmt->bindParam("calendar_id", $id);
$stmt->execute();
//$user->id = $db->lastInsertId();
$db = null;
//echo json_encode($user);
} catch (PDOException $e) {
error_log($e->getMessage(), 3, 'C:\windows\temp\php.log');
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}