Error: I need help with MVC


#1

You are giving the following error:
Notice : Undefined variable: app in C:\xampp\htdocs\thesi\admin\services.php on line 6

Fatal error : Uncaught Error: Call to a member function get() on null in C:\xampp\htdocs\thesi\admin\services.php:6 Stack trace: #0 {main} thrown in C:\xampp\htdocs\thesi\admin\services.php on line 6

I do not know how to solve, could someone help me?

Below is the admin folder with the

Bootstrap

<?php
use Illuminate\Database\Capsule\Manager as Db;
include __DIR__ . '/../vendor/autoload.php';

$db = new Db();

$db->addConnection([
    'driver' => 'pgsql',
    'host' => 'localhost',
    'database' => 'dbescola',
    'username' => 'postgres',
    'password' => 'ana1997',
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix' => ''
]);

$db->setAsGlobal();
$db->bootEloquent();

include __DIR__ . '/app.php'; //nova linha

Services

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Illuminate\Database\Capsule\Manager as Db;

$app->get('/aluno/', function ()  {
    $aluno = Db::table('aluno')->get();
    return json_encode($aluno);
});

app

<?php
use Illuminate\Database\Capsule\Manager as Db;

$app = new Slim\App;

// autenticação
$app->add(new Tuupola\Middleware\HttpBasicAuthentication([
    "users" => [
        "anam" => "anam",
        "anap" => "anap"
    ]
]));

include __DIR__ . "/services.php";

$app->run();

#2

Im a little bit confused her?!.. Your Services.php looks like a route file? If thats the case, I would advice you to change your setup, so that your route calls a method in a controller instead of trying to query the database directly from your route…