<?php $container = (require 'Container.php'); $app = new \Slim\App($container); $app->group('/api/v1', function () use($app) { $app->any('/data/{table}[/{id}]', function ($req, $res, $args) { $middleware = $this->get('retrieveData'); return $middleware($req, $res); })->add('parser'); }); $app->run();
* @since: 1/2/2016. * @license: * @version 0.1 */ # IMPORT require __DIR__ . "/vendor/autoload.php"; use KarabowId\Api\Orm\OrmManager; use KarabowId\Api\ParamHandler; use KarabowId\Api\Messages; # SETUP $configuration = ['settings' => ['displayErrorDetails' => true]]; $config = new \Slim\Container($configuration); $app = new Slim\App($config); $ormManager = new OrmManager(); $app->any("/", function ($request, $response, $args) { $reponse->getBody()->write("No Request Made. Should we throw an exception? or just tell the user to go learn how to consume this api?"); }); # CREATE NEW USER $app->post("/user/new", function ($request, $response, $args) use($app) { return $response; }); # GET USER INFO $app->get("/user", function ($request, $response, $args) use($app) { return $response; }); # MODIFY USER INFO $app->put("/user/edit", function ($request, $response, $args) use($app) { return $response; }); # DELETE USER FROM DB $app->delete("/user/delete", function ($request, $response, $args) use($app) {
$app->get('/', function ($request, $response, $args) { $response->write("Welcome to Slim!"); return $response; }); $app->get('/hello[/{name}]', function ($request, $response, $args) { $response->write("Hello, " . $args['name']); return $response; })->setArgument('name', 'World!'); $app->any('/test', function () use($app) { //GET $allGetVars = $request->getQueryParams(); print_r($allGetVars); foreach ($allGetVars as $key => $param) { //GET parameters list } //POST or PUT $allPostPutVars = $request->getParsedBody(); print_r($allPostPutVars); if (!empty($allPostPutVars)) { foreach ($allPostPutVars as $key => $param) { //POST or PUT parameters list } } }); $app->any('/auth', function ($request, $response, $args) { // GET, POST or PUT $params = getParams($request); // Check for required params verifyRequired(array('email', 'password'), $params); // Validating email address validateEmail($params['email']); if ($params['email'] == '*****@*****.**' && $params['password'] == '123456') {
*/ $app->add(function (Request $request, Response $response, callable $next) { $uri = $request->getUri(); $path = $uri->getPath(); if ($path != '/' && substr($path, -1) == '/') { $uri = $uri->withPath(substr($path, 0, -1)); return $response->withRedirect((string) $uri, 301); } return $next($request, $response); }); /** * Handle request and process response. * * Loads controller based endpoint and calls the controller's method based on the * request method. * */ $app->any('/[{controller}[/{segments:.+}]]', function (Request $request, Response $response, $args) { if (!count($args)) { $args['controller'] = strtolower(\App\Controller\Controller::DEFAULT_CONTROLLER); } $class_name = sprintf('App\\Controller\\%s', stringToStudlyCaps($args['controller'])); if (class_exists($class_name)) { $controller = new $class_name($request, $response); $response = $controller->getRequestResponse(); } else { $response = $response->withStatus(404)->write('Not Found'); } return $response; }); $app->run();
<?php require_once 'vendor/autoload.php'; $app = new \Slim\App(); $app->add(\PaulJulio\SlimExample\JSON::class); $app->any('/', function ($request, $response, $args) { $body = $response->getBody(); $body->offsetSet('c', 3); return $response->withBody($body); }); $app->add(\PaulJulio\SlimExample\Author::class); $app->run();
$response = $response->withHeader('Content-type', 'application/json'); $body = $response->getBody(); $body->write(json_encode(array('success' => true, 'message' => 'You accessed my APIs!'))); return $response; }); $app->any('/oauth/authorize', function (Req $request, Res $response) use($server) { $oauthRequest = OAuth2\Request::createFromGlobals(); $aouthResponse = new OAuth2\Response(); // validate the authorize request if (!$server->validateAuthorizeRequest($oauthRequest, $aouthResponse)) { $aouthResponse->send(); return $response; } if (empty($_POST)) { exit('<form method="post"> <label>Do You Authorize TestClient?</label><br /> <input type="submit" name="authorized" value="yes"> <input type="submit" name="authorized" value="no"> </form>'); } // print the authorization code if the user has authorized your client $is_authorized = $_POST['authorized'] === 'yes'; $server->handleAuthorizeRequest($oauthRequest, $aouthResponse, true); if ($is_authorized) { $aouthResponse->send(); die('Redirect'); } $error_uri = $_GET['redirect_uri'] . '?' . http_build_query(['error' => 'Access denied', 'error_description' => 'user did not grand access']); return $response->withStatus(301)->withHeader('Location', $error_uri); }); $app->run();
/** * Divert to login if not admin * @param Protect $session */ function ssp_logon($session) { if (!$session->admin) { SSP_Divert($session->cfg->logonScript); } } $app = new \Slim\App($container); // home page $app->any('/', function (Request $request, Response $response) { /* @var $session Protect */ $session = $this->session; ssp_logon($session); $ssp = $this->ssp; $lister = new UserLister($ssp); return $response->getBody()->write($lister->lister()); }); // delete a user $app->any('/delete/{userId}', function (Request $request, Response $response) { /* @var $session Protect */ $session = $this->session; ssp_logon($session); $ssp = $this->ssp; $userId = $request->getAttribute('userId', ''); $lister = new UserLister($ssp); return $response->getBody()->write($lister->deleteUser($userId)); }); // change filter $app->any('/filterChange', function (Request $request, Response $response) {