/** * Test status */ public function testStatus() { $s = new \Slim\Slim(); $s->get('/bar', function () use($s) { $s->status(403); }); $s->call(); $this->assertEquals(403, $s->response()->status()); }
$response['status'] = "success"; $response['message'] = 'Logged in successfully.'; $response['name'] = 'admin'; } else { $response['status'] = "error"; $response['message'] = 'Incorrect credentials'; } } else { $response['status'] = "error"; $response['message'] = 'No such user is registered'; } echoResponse(200, $response); }); $app->get('/department/:id', function ($id) use($app) { // Http response code $app->status(200); // setting response content type to json $app->contentType('application/json'); switch ($id) { case 'volvo': echo '[{"id":1, "name":"commerciale"},{"id":2,"name":"innovation"}]'; break; case 'saab': echo '[{"id":1, "name":"commerciale"},{"id":2,"name":"financier"}]'; break; case 'opel': echo '[{"id":1, "name":"commerciale"},{"id":2,"name":"administratif"}]'; break; case 'audi': echo '[{"id":1, "name":"commerciale"},{"id":2,"name":"produit"},{"id":3,"name":innovation}]'; break;
private function runAppPreFlight($action, $actionName, $mwOptions = NULL, $headers = array()) { \Slim\Environment::mock(array('REQUEST_METHOD' => 'OPTIONS', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => 80, 'ACCEPT' => 'application/json', 'SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/' . $actionName)); $app = new \Slim\Slim(); $app->setName($actionName); $mw = function () { // Do nothing }; if (isset($mwOptions)) { if (is_callable($mwOptions)) { $mw = $mwOptions; } else { $mwOptions['appName'] = $actionName; $mw = \CorsSlim\CorsSlim::routeMiddleware($mwOptions); } } $app->options('/:name', $mw, function ($name) use($app, $action) { }); $app->delete('/:name', $mw, function ($name) use($app, $action) { if ($app->request->isHead()) { $app->status(204); return; } $app->contentType('application/json'); $app->response->write(json_encode(array("action" => $action, "method" => "DELETE", "name" => $name))); }); foreach ($headers as $key => $value) { $app->request->headers()->set($key, $value); } $app->run(); return $app; }
require_once 'env.php'; require_once APP_DIR . '/vendor/autoload.php'; $app = new \Slim\Slim(array('debug' => false, 'cookies.encrypt' => true, 'cookies.secret_key' => 'd3@SD#@!TXZE@', 'cookies.cipher' => MCRYPT_RIJNDAEL_256, 'cookies.cipher_mode' => MCRYPT_MODE_CBC, 'log.enabled' => true, 'log.writer' => new src\common\LogWriter(), 'log.level' => \Slim\Log::DEBUG)); //处理request数据类型 $app->add(new \Slim\Middleware\ContentTypes()); $app->notFound(function () use($app) { $app->getLog()->warning('url not found:' . $app->request()->getResourceUri()); $app->render('404.html'); }); //处理所有未catch exception $app->error(function (Exception $e) use($app) { $app->getLog()->critical('server error: ' . $e->getMessage()); $app->halt(500, "sorry! server error"); }); $request = $app->request(); $paths = explode('/', $request->getResourceUri()); if (count($paths) < 4 || strtolower($paths[1]) != 'api') { $app->getLog()->error('bad request:' . $request->getResourceUri()); $app->status(400); } $app->group('/api', function () use($app, $paths) { $router = ucfirst(strtolower($paths[2])); if (!file_exists(APP_DIR . "/src/routers/{$router}.php")) { return; } $app->group("/{$paths['2']}", function () use($app, $router) { $routerClass = "src\\routers\\{$router}"; new $routerClass($app); }); }); $app->run();
<?php require_once "../vendor/autoload.php"; $config = (require_once "config.php"); $pdo = new PDO($config['dsn'], $config['username'], $config['password']); $db = new NotORM($pdo); $app = new Slim\Slim(); $app->response->headers->set('Content-Type', 'application/json'); // Set default status code (403 Forbidden), if no routes are followed $app->status(403); /** Routes */ /** Say hello */ $app->get('/hello', function () use($app) { $app->status(200); echo "Hello!"; }); // Authorize $app->get('/authorize', function () use($app) { $app->status(501); }); $app->run();
private function runAppPreFlight($action, $actionName, $mw = NULL, $headers = array()) { \Slim\Environment::mock(array('REQUEST_METHOD' => 'OPTIONS', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => 80, 'ACCEPT' => 'application/json', 'SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/' . $actionName)); $app = new \Slim\Slim(); if (isset($mw)) { $app->add($mw); } $app->delete('/:name', function ($name) use($app, $action) { if ($app->request->isHead()) { $app->status(204); return; } $app->contentType('application/json'); $app->response->write(json_encode(array("action" => $action, "method" => "DELETE", "name" => $name))); }); foreach ($headers as $key => $value) { $app->request->headers()->set($key, $value); } $app->run(); return $app; }