[SOLVED] Problem loading php files with require in file index.php

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?

It looks like each of your route files is creating a new instance of $app?

1 Like

This is the part where routing magic happens:

So based on that, you can build the routing to the other url calls, so the result will be something like:

$app = new \Slim\App;
$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});
$app->get('/api/vote/stage/{stageid}}', function (Request $request, Response $response) {
    $stageid= $request->getAttribute('stageid');
    // do your magic here
    (my advice is to have a stage class that can handle all stageid variants, and returns a result)
   $response = new YourMagic($stageid);

    return $response;
});
1 Like

thanks, I ran it by removing $app=new\Slim\App;

the files with require were:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;


$app->get('/api/vote/stage/1', function(Request $request, Response $response){
    $sql = 'SELECT `idstage_one` AS NUMBER,