Skip to content

Instantly share code, notes, and snippets.

@aonurdemir
Created January 4, 2018 07:53
Show Gist options
  • Save aonurdemir/5934aadf55fb8aeeb0941ad31d6614f8 to your computer and use it in GitHub Desktop.
Save aonurdemir/5934aadf55fb8aeeb0941ad31d6614f8 to your computer and use it in GitHub Desktop.
Slim functions
Redirect
return $res->withRedirect("/login");
Get GET parameters
$jobId = $req->getQueryParams()['jobId']; //checks _GET [IS PSR-7 compliant]
Get POST parameters
$filename = $req->getParsedBody()['filename'];
Sanitize query param
$jobId = filter_var($jobId, FILTER_SANITIZE_NUMBER_INT);
DB access
/**@var PDO $db */
$db = $this->get("mysql-archiveapp");
$sql = "SELECT * FROM job where id = :id";
$stmt = $db->prepare($sql);
$stmt->bindValue(":id", $jobId);
$stmt->execute();
DB Fetch
$stmt->fetch()
Download file
$fp = $task->getReport()->getReportFilePointer();
$stream = new \Slim\Http\Stream($fp); // create a stream instance for the response body
return $res->withHeader('Content-Type', 'application/force-download')
->withHeader('Content-Type', 'application/octet-stream')
->withHeader('Content-Type', 'application/download')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Content-Disposition', 'attachment; filename="' . basename($task->getReport()->getFilename()) . '.csv"')
->withHeader('Expires', '0')
->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
->withHeader('Pragma', 'public')
->withBody($stream); // all stream contents will be sent to the response
Return body
$res->getBody()->write("file not found");
Render view
$this->view->render($res, "query.phtml",[//args]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment