error loading files from the routes folder only loads a single file from all three with require
file: index.php
<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; require 'vendor/autoload.php'; require 'src/config/db.php'; $app = new \Slim\App; $app->get('/hello/{name}', function (Request $request, Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response; }); // votes for stage require 'src/routes/votes/stage_one_vote.php'; //<-- only upload the methods of this file require 'src/routes/votes/stage_two_vote.php'; require 'src/routes/votes/stage_three_vote.php'; $app->run();
file: stage_one_vote.php
route file: ‘src/routes/votes/stage_one_vote.php’
<?php use \Psr\Http\Message\ServerRequestInterface as Request; use \Psr\Http\Message\ResponseInterface as Response; $app = new \Slim\App; $container = $app->getContainer(); $app->options('/{routes:.+}', function ($request, $response, $args) { return $response; }); $app->add(function ($req, $res, $next) { $response = $next($req, $res); return $response ->withHeader('Access-Control-Allow-Origin', '*') ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Accept, Origin, Authorization') ->withHeader('Content-Type', 'application/json') ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); }); $app->get('/api/vote/stage/1', function(Request $request, Response $response){ $sql = 'SELECT `idstage_one` AS NUMBER, CONCAT(models.name," ", models.last_name) AS name, `date` FROM `stage_one_vows` INNER JOIN models ON models.idmodel = stage_one_vows.idmodel'; try{ // Get DB Object $db = new db(); // Connect $db = $db->connect(); $stmt = $db->query($sql); $data = $stmt->fetchAll(PDO::FETCH_OBJ); $db = null; if($data && $data!=null) { return $response->withStatus(200) ->write(json_encode($data)); } else { throw new PDOException('No records found');} } catch(PDOException $e){ $err = '{"error": {"text": "'.$e->getMessage().'"}}'; return $response->withStatus(400) ->write($err); } }); ... ?>
the other files have an identical structure only differs in the example routes the path in the file stage_two_vote.php is $ app->get (‘/api /vote/stage /2’
how can I load multiple files so I do not lengthen the code a lot in a single file?