public function deleteAction(Request $request, Application $app) { $data = UtilsService::checkJsonStructure($request); if ($data === -1 || $data === NULL) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::FATAL_ERROR_STATUS_CODE, ResponseMessagesAndStatuses::JSON_FORMAT_WRONG_MESSAGE)); } $checkMissingDataOrSendResponse = UtilsService::checkRequestParamsMissing($data, array('id'), $app); if ($checkMissingDataOrSendResponse !== -1) { return $checkMissingDataOrSendResponse; } try { $app['repository.phone_details']->delete($request->get('id')); } catch (\Exception $e) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::FATAL_ERROR_STATUS_CODE, $e->getMessage())); } return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::SUCCESS_STATUS_CODE, ResponseMessagesAndStatuses::DELETED_ROW_MESSAGE)); }
public function loginAction(Request $request, Application $app) { $data = UtilsService::checkJsonStructure($request); if ($data === -1 || $data === NULL) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::FATAL_ERROR_STATUS_CODE, ResponseMessagesAndStatuses::JSON_FORMAT_WRONG_MESSAGE)); } $checkMissingDataOrSendResponse = UtilsService::checkRequestParamsMissing($data, array('username', 'password'), $app); if ($checkMissingDataOrSendResponse !== -1) { return $checkMissingDataOrSendResponse; } try { $resultData = $app['repository.user']->authenticate($data, $app); if (!$resultData) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::MISSING_PARAMS_STATUS_CODE, ResponseMessagesAndStatuses::INVALID_CREDENTIALS)); } } catch (\Exception $e) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::FATAL_ERROR_STATUS_CODE, $e->getMessage())); } return UtilsService::createAndSendResponse($app, $resultData, true); }
$app = new Silex\Application(); $app['debug'] = true; // Registering repo for phone_details db table $app['repository.phone_details'] = $app->share(function ($app) { return new PhoneDirectory\Repository\PhoneBookRepository($app['db']); }); // Registering repo for user db table $app['repository.user'] = $app->share(function ($app) { return new PhoneDirectory\Repository\UserRepository($app['db']); }); // Registering different providers required $app->register(new Silex\Provider\DoctrineServiceProvider(), array('db.options' => array('driver' => 'pdo_mysql', 'host' => '127.0.0.1', 'port' => '3306', 'dbname' => 'phone_book', 'user' => 'root', 'password' => ''))); $app->register(new Silex\Provider\SessionServiceProvider(), array('session.storage.save_path' => dirname(__DIR__) . '/tmp/sessions')); $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.options' => array('cache' => isset($app['twig.options.cache']) ? $app['twig.options.cache'] : false, 'strict_variables' => true), 'twig.form.templates' => array('form_div_layout.html.twig', 'common/form_div_layout.html.twig'), 'twig.path' => array(__DIR__ . '/../app/views'))); $app->register(new Silex\Provider\MonologServiceProvider(), array('monolog.logfile' => __DIR__ . '/development.log')); // Pre execute condition before any controller of API $app->before(function (Request $request, Application $app) { $request->getSession()->start(); if ($request->getRequestUri() != "/api/user/login" && $request->getRequestUri() != "/") { if (!$app['session']->get('csrf') || $app['session']->get('csrf') != $request->get("csrf")) { return UtilsService::createAndSendResponse($app, array(ResponseMessagesAndStatuses::REDIRECT_STATUS_CODE, ResponseMessagesAndStatuses::REQUEST_NOT_ALLOWED)); } } }); // Register the error handler. $app->error(function (\Exception $e, $code) use($app) { $response = json_encode(array("code" => $code, "message" => $e->getMessage())); $app['monolog']->addError($code . "==" . $e->getMessage()); return new Response($response, $code); }); return $app;