/** * [PUT] /$resource/$id * @param string $resource * @param int $id * @return type */ public function putAction($resource, $id) { $modelClass = $this->factory->with($resource); $model = $modelClass->find($id); if (is_null($model)) { $this->render(['error' => 'Not found'], 404); } else { $env = $this->app->environment(); if (empty($env['slim.input'])) { $this->render(['error' => 'Data not valid'], 400); } else { $model->fill($env['slim.input']); $model->save(); $this->render($model->toArray()); } } }
// LOADING FRAMEWORKS require_once __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'Tools' . DIRECTORY_SEPARATOR . 'Autoloader.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'defines' . DIRECTORY_SEPARATOR . 'defines.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'defines' . DIRECTORY_SEPARATOR . 'gamevars.php'; require_once __DIR__ . DIRECTORY_SEPARATOR . 'php' . DIRECTORY_SEPARATOR . 'defines' . DIRECTORY_SEPARATOR . 'configuration.php'; // INIT ERROR-HANDLING && LOGGING global $log4php_config; \Logger::configure($log4php_config); $logger = \Logger::getLogger('index.php'); // INIT SLIM APP $app = new Slim(array('debug' => $debug, 'view' => new Twig())); $view = $app->view(); $view->parserExtensions = array(new \Twig_Extension_Debug()); $view->parserOptions = array('debug' => $debug); $env = $app->environment(); $env['basepath'] = __DIR__; // DEFINE SLIM-ERROR HANDLING $app->error(function (\Exception $e) use($app, $logger) { $logger->error($e->getMessage()); $data = array(); HeaderViewHelper::parseCurrentUser($data); $app->render('error.twig', $data); }); $app->notFound(function () use($app) { $data = array(); HeaderViewHelper::parseCurrentUser($data); $app->render('404.twig', $data); }); // INIT DB-CONNECTION try {
/** * Serialize the data based on the provided "Accept" header if it exists, * otherwise revert to the predefined default content-type * * @param \Slim\Slim $app * @param $data * * @return array|mixed */ public static function getSerializedData(Slim $app, $data) { self::$contentTypes = $app->config("serializers"); $env = $app->environment(); $acceptableContentTypes = explode(";", $env["ACCEPT"]); $contentType = ""; if (count($acceptableContentTypes) > 1 || empty($acceptableContentTypes)) { $contentType = $app->config("content-type"); } else { $contentType = $acceptableContentTypes[0]; } // don't allow */* as the content-type, rather favour the default content-type if ($contentType == "*/*") { $contentType = $app->config("content-type"); } $app->contentType($contentType); $data = self::serialize($app, $data, $contentType); return $data; }
Resource::error($code, $e->getMessage()); }); // Database layer setup $app->hook('slim.before', function () use($app) { $app->container->singleton('mongo', function () use($app) { $client = new Client($app->config('database')['host_uri']); $client->map([$app->config('database')['db_name'] => '\\API\\Collection']); $client->useDatabase($app->config('database')['db_name']); return $client; }); }); // CORS compatibility layer (Internet Explorer) $app->hook('slim.before.router', function () use($app) { if ($app->request->isPost() && $app->request->get('method')) { $method = $app->request->get('method'); $app->environment()['REQUEST_METHOD'] = strtoupper($method); mb_parse_str($app->request->getBody(), $postData); $parameters = new Set($postData); if ($parameters->has('content')) { $content = $parameters->get('content'); $app->environment()['slim.input'] = $content; $parameters->remove('content'); } else { // Content is the only valid body parameter...everything else are either headers or query parameters $app->environment()['slim.input'] = ''; } $app->request->headers->replace($parameters->all()); $app->environment()['slim.request.query_hash'] = $parameters->all(); } }); // Parse version