There is an index.php file that accepts all requests and processes each callback with a callback.
require 'flight/Flight.php'; Flight::route('GET /', function () { Flight::render('home', array('body_class' => 'frontpage')); }); Flight::route('GET /images', function(){ $sql = "SELECT * FROM images"; try{ $db = getConnection(); $stmt = $db->prepare($sql); $stmt->execute(); $stmt->setFetchMode(PDO::FETCH_ASSOC); $result = json_encode($stmt->fetchAll()); $db = null; echo $result; }catch(PDOException $e){ echo $e->getMessage(); } }); Flight::start(); function getConnection() { $dbhost = "127.0.0.1"; $dbuser = "root"; $dbpass = ""; $dbname = "test"; $dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING); return $dbh; }
Can I set my own header in each callback? For example, in the second root I need JSON, and in the first HTML.