public function register(App &$app) { $self = $this; $app->post('/raids', function (Request $request, Response $response) use(&$self) { return $self->responseRaids($request, $response)->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Content-type', 'application/json'); }); $app->options('/raids', function (Request $request, Response $response, $args) use(&$self) { return $response->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Access-Control-Allow-Headers', 'Content-Type, X-AUTH-USER, X-AUTH-TOKEN'); }); $app->post('/raids/add', function (Request $request, Response $response) use(&$self) { return $self->responseRaidsAdd($request, $response)->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Content-type', 'application/json'); }); $app->options('/raids/add', function (Request $request, Response $response, $args) use(&$self) { return $response->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Access-Control-Allow-Headers', 'Content-Type, X-AUTH-USER, X-AUTH-TOKEN'); }); }
public function register(App &$app) { $self = $this; $app->post('/login', function (Request $request, Response $response) use(&$self) { return $self->responseLogin($request, $response)->withHeader('Content-type', 'application/json')->withHeader('Access-Control-Allow-Origin', '*'); }); $app->options('/login', function (Request $request, Response $response, $args) use(&$self) { return $response->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Access-Control-Allow-Headers', 'Content-Type'); }); $app->post('/login/check', function (Request $request, Response $response) use(&$self) { return $self->responseLoginCheck($request, $response)->withHeader('Content-type', 'application/json')->withHeader('Access-Control-Allow-Origin', '*'); }); $app->options('/login/check', function (Request $request, Response $response, $args) use(&$self) { return $response->withHeader('Access-Control-Allow-Origin', '*')->withHeader('Access-Control-Allow-Headers', 'Content-Type'); }); }
public function testPostRoute() { $path = '/foo'; $callable = function ($req, $res) { // Do something }; $app = new App(); $route = $app->post($path, $callable); $this->assertInstanceOf('\\Slim\\Route', $route); $this->assertAttributeContains('POST', 'methods', $route); }
/** * @param string $method * @param string $route * @param object $routerClass * @param string $callback * @param Secured $secured * @throws \Exception */ private function setRoute($method, $route, $routerClass, $callback, $secured) { $method = strtoupper($method); $easyRoute = new Route($route, $routerClass, $callback, $secured, $this); if ($method === 'GET') { $this->app->get($route, array($easyRoute, 'call')); } elseif ($method === 'PUT') { $this->app->put($route, array($easyRoute, 'call')); } elseif ($method === 'POST') { $this->app->post($route, array($easyRoute, 'call')); } elseif ($method === 'DELETE') { $this->app->delete($route, array($easyRoute, 'call')); } else { throw new \Exception('Unsupported HTTP method ' . $method); } }
*/ // config $debug = false; if (defined("DEBUG")) { $debug = true; } // Make a Slim App // $app = new App($c) $app = new App(['settings' => ['debug' => $debug, 'whoops.editor' => 'sublime']]); $app->add(new WhoopsMiddleware()); // Home $app->get('/', 'App\\Controllers\\HomeController:index'); $app->get('/code', 'App\\Controllers\\HomeController:code'); $app->get('/tos', 'App\\Controllers\\HomeController:tos'); $app->get('/debug', 'App\\Controllers\\HomeController:debug'); $app->post('/debug', 'App\\Controllers\\HomeController:postDebug'); // User Center $app->group('/user', function () { $this->get('', 'App\\Controllers\\UserController:index'); $this->get('/', 'App\\Controllers\\UserController:index'); $this->post('/checkin', 'App\\Controllers\\UserController:doCheckin'); $this->get('/node', 'App\\Controllers\\UserController:node'); $this->get('/node/{id}', 'App\\Controllers\\UserController:nodeInfo'); $this->get('/profile', 'App\\Controllers\\UserController:profile'); $this->get('/invite', 'App\\Controllers\\UserController:invite'); $this->post('/invite', 'App\\Controllers\\UserController:doInvite'); $this->get('/edit', 'App\\Controllers\\UserController:edit'); $this->post('/password', 'App\\Controllers\\UserController:updatePassword'); $this->post('/sspwd', 'App\\Controllers\\UserController:updateSsPwd'); $this->post('/method', 'App\\Controllers\\UserController:updateMethod'); $this->get('/sys', 'App\\Controllers\\UserController:sys');
$data = ['message' => $exception->getMessage()]; return $container->get('response')->withStatus(500)->withHeader('Content-Type', 'application/json')->write(json_encode($data)); }; }; //Register authentication container Dependency $container['auth'] = function ($container) { return new BB8\Emoji\Auth($container); }; //Initialize the slim app $app = new App($container); //Add middleware at app level $app->add('BB8\\Emoji\\Middleware:init'); //Index page $app->get('/', 'BB8\\Emoji\\Controllers\\UserController:index'); //Create new user $app->post('/signup', 'BB8\\Emoji\\Controllers\\UserController:create'); //Login Route $app->post('/auth/login', 'BB8\\Emoji\\Controllers\\UserController:login'); //Logout Route $app->get('/auth/logout', 'BB8\\Emoji\\Controllers\\UserController:logout')->add('BB8\\Emoji\\Middleware:authorize'); //List all emojis Route $app->get('/emojis', 'BB8\\Emoji\\Controllers\\EmojiController:index'); //Gets an emoji $app->get('/emojis/{id}', 'BB8\\Emoji\\Controllers\\EmojiController:show'); //Adds a new Emoji $app->post('/emojis', 'BB8\\Emoji\\Controllers\\EmojiController:create')->add('BB8\\Emoji\\Middleware:authorize'); //Updates an Emoji $app->put('/emojis/{id}', 'BB8\\Emoji\\Controllers\\EmojiController:update')->add('BB8\\Emoji\\Middleware:authorize'); //Updates an Emoji Keyword $app->put('/emojis/{id}/{kId}', 'BB8\\Emoji\\Controllers\\EmojiController:updateKey')->add('BB8\\Emoji\\Middleware:authorize'); //Partially Updates an Emoji
require_once 'src/zelory/diskonmania/DB.php'; $container = new Container(); $container['foundHandler'] = function () { return new RequestResponseArgs(); }; $app = new App($container); $app->get('/', function (Request $request, Response $response) { $response->getBody()->write('<script type="text/javascript">url = window.location.href + "doc"; window.location = url;</script>'); return $response->withStatus(200)->withHeader('Content-Type', 'text/html'); }); $app->post('/login', function (Request $request, Response $response) { try { $params = $request->getQueryParams(); $user = User::login($params['username'], $params['password']); if ($user == null) { throw new Exception("Invalid username or password!"); } return ResultWrapper::getResult($user, $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response); } }); $app->post('/register', function (Request $request, Response $response) { try { $params = $request->getQueryParams(); $user = User::register($params['username'], $params['name'], $params['password']); if ($user == null) { throw new Exception("Invalid username or password!"); } return ResultWrapper::getResult($user, $response); } catch (Exception $e) { return ResultWrapper::getError($e->getMessage(), $response);
/** * Add a repository to your Slim App * @param App $app * @param Repository $repository * @return App the given app for chaining */ public function bootStrap(App $app, Repository $repository) { $baseUrl = '/' . self::parseRepoName($repository->getEntityClass()->getShortName()); /** * Get the whole collection. */ $app->get($baseUrl, function (Request $request, Response $response) use($repository) { return $response->write(self::output($repository->findAll()))->withHeader('Content-Type', 'application/json'); }); /** * Delete the whole collection. */ $app->delete($baseUrl, function (Request $request, Response $response) use($repository) { $repository->deleteAll(); }); /** * Add a new entity to the collection. */ $app->post($baseUrl, function (Request $request, Response $response) use($repository) { $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response); if ($body instanceof Response) { return $body; } else { // Store the entity $repository->insert($body); return $response->withStatus(Status::CREATED)->withHeader('Content-Type', 'application/json')->write(self::output($body)); } }); if ($this->showCheckPage) { /** * Display the repository check page. */ $app->get($baseUrl . '/check', function (Request $request, Response $response) use($repository) { $repository->checkDatabase(); }); } $entityUrl = $baseUrl . '/{id}'; /** * Get a single entity. */ $app->get($entityUrl, function (Request $request, Response $response, $args) use($repository) { $entity = $repository->get($args['id']); if ($entity) { return $response->write(self::output($entity))->withHeader('Content-Type', 'application/json'); } return $response->withStatus(Status::NOT_FOUND); }); /** * Delete a single entity */ $app->delete($entityUrl, function (Request $request, Response $response, $args) use($repository) { $repository->delete($args['id']); }); /** * Replace a single entity */ $app->put($entityUrl, function (Request $request, Response $response, $args) use($repository) { $body = self::getBody($request->getBody(), $repository->getEntityClass(), $response); if ($body instanceof Response) { return $body; } else { // Store the entity $repository->getIdProperty()->setValue($body, $args['id']); $repository->update($body); return $response->withHeader('Content-Type', 'application/json')->write(self::output($body)); } }); return $app; }
$app->post('/', function (\Slim\Http\Request $request, \Slim\Http\Response $response, array $args) { $postParams = $request->getParsedBody(); /** @var \Slim\Flash\Messages $flash */ $flash = $this->get('flash'); try { \Assert\lazy()->that($postParams['email'], 'email')->notEmpty('Email must be provided')->email('Email must be valid')->verifyNow(); } catch (Assert\LazyAssertionException $e) { $flash->addMessage('error', $e->getMessage()); return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('getForm') . '?' . http_build_query(['email' => $postParams['email']])); } /** @var ExtendedPdo $pdo */ $pdo = $this->get('pdo'); $stm = "SELECT * FROM indiegogo WHERE Email = :email"; $bind = ['email' => $postParams['email']]; $sth = $pdo->perform($stm, $bind); $result = $sth->fetch(PDO::FETCH_ASSOC); if (!$result) { $flash->addMessage('error', 'No entry found for that email'); return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('getForm') . '?' . http_build_query(['email' => $postParams['email']])); } try { if ($result['Perk ID'] == 3613098) { \Assert\lazy()->that($postParams, 'Shirt Type')->keyExists('shirtType', 'You must select a shirt type')->notEmptyKey('shirtType', 'You must select a shirt type')->that($postParams, 'Shirt Size')->keyExists('shirtSize', 'You must select a shirt size')->notEmptyKey('shirtSize', 'You must select a shirt size')->verifyNow(); } elseif ($result['Perk ID'] == 3633662) { \Assert\lazy()->that($postParams, 'Hoodie Size')->keyExists('hoodieSize', 'You must select a hoodie size')->notEmptyKey('hoodieSize', 'You must select a hoodie size')->verifyNow(); } } catch (Assert\LazyAssertionException $e) { /** @var \Assert\InvalidArgumentException[] $errorExceptions */ $errorExceptions = $e->getErrorExceptions(); foreach ($errorExceptions as $errorException) { $flash->addMessage('error', $errorException->getMessage()); } return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('getForm') . '?' . http_build_query(['email' => $postParams['email']])); } catch (\Exception $e) { $flash->addMessage('error', $e->getMessage()); return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('getForm') . '?' . http_build_query(['email' => $postParams['email']])); } /* * Save results and redirect */ try { $qf = new QueryFactory('mysql'); /** @var SqlQuery\Mysql\Update $update */ $update = $qf->newUpdate(); $update->table('indiegogo')->where('Email = :email')->bindValue('email', $postParams['email']); if (!empty($postParams['shirtType'])) { $update->set('shirtType', ':shirtType')->bindValue('shirtType', $postParams['shirtType']); } if (!empty($postParams['shirtSize'])) { $update->set('shirtSize', ':shirtSize')->bindValue('shirtSize', $postParams['shirtSize']); } if (!empty($postParams['hoodieSize'])) { $update->set('hoodieSize', ':hoodieSize')->bindValue('hoodieSize', $postParams['hoodieSize']); } /** @var ExtendedPdo $pdo */ $pdo = $this->get('pdo'); $stm = $update->getStatement(); $sth = $pdo->perform($stm, $update->getBindValues()); } catch (\Exception $e) { $flash->addMessage('error', 'Problem updating: exception thrown when updating'); return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('getForm') . '?' . http_build_query(['email' => $postParams['email']])); } $flash->addMessage('info', 'Preferences saved. We will ship your perk ASAP!'); return $response->withStatus(302)->withHeader('Location', $this->router->pathFor('getForm') . '?' . http_build_query(['email' => $postParams['email']])); })->setName('postForm');
use Psr\Http\Message\ResponseInterface as Response; $container = new Container(); $app = new App($container); $app->post('/', function (Request $request, Response $response) { $fileName = $_FILES['file']['name']; $pathInfo = pathinfo($fileName); $extension = $pathInfo['extension']; // TODO: Do some validation here for extension type $fileConverter = new \PNGify\FileConverter($_FILES['file']['tmp_name'], $extension); $config = (require './config.php'); $fileConverter->setMappings($config['extensions']); try { $image = $fileConverter->toImage(); } catch (\PNGify\Exceptions\InvalidFileExtension $e) { $response = $response->withStatus(400); /** @var \Psr\Http\Message\StreamInterface $body */ $body = $response->getBody(); $body->write(json_encode(['error' => 'Invalid file extension'])); $response = $response->withHeader('Content-Type', 'application/json'); $response = $response->withHeader('Content-Disposition', 'inline; filename="' . $pathInfo['filename'] . '.png"'); $response = $response->withBody($body); return $response; } $response = $response->withHeader('Content-Type', 'image/png'); $body = $response->getBody(); $body->write($image); $response = $response->withBody($body); return $response; }); $app->get('/', function (Request $request, Response $response) { $body = $response->getBody(); $body->write(file_get_contents('./static/templates/index.html'));
use Psr\Http\Message\ResponseInterface as Response; use Slim\App; // Initialize server require 'vendor/autoload.php'; require 'Message.php'; $app = new App(); $database = new medoo(['database_type' => 'sqlite', 'database_file' => './database.sqlite']); $message = new Message($database); /* * Define server API endpoints */ // Main UI $app->get('/', function (Request $request, Response $response) { return readfile('interface.html'); }); // Update chat endpoint for polling // returns all messages after a specific timestamp $app->get('/chat/{timestamp:\\d+}', function (Request $request, Response $response, $args) use($message) { $messages = $message->getAfter((int) $args['timestamp']); $response = $response->withHeader('Content-Type', 'application/json'); return $response->getBody()->write(json_encode($messages)); }); // Main chat function: emit a message to the global chat // messages take the JSON format {"user": "******", "message": "hello world"} $app->post('/chat', function (Request $request, Response $response) use($message) { $data = $request->getParsedBody(); $message->create($data['name'], $data['message']); return $response->withStatus(204); }); // Start the application $app->run();