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.

    1 answer 1

    By experiment, I found out that it is possible to write several headers in one file. In this case, in the second callback, I can set the header on application / json, in all other cases it will be just text / html by default.

    • one
      The fact that you have written the code does not mean that it will be executed. Everything that lies inside the unprovoked functions and code blocks, whose conditions were not satisfied, will not be fulfilled, and, as far as I understand, will not be able to in any way affect the course of the program. - etki