Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKennard McGill
Full Stack JavaScript Techdegree Graduate 43,179 PointsFatal Error: After following all the steps to this point. What's going on?
I followed the video twice and I still ended up with a Fatal Error message. Uncaught Error: Function name must be a string in C:\xampp\htdocs\treehouse\php_rest_api\public\index.php:22 Stack trace: #0 {main} thrown in C:\xampp\htdocs\treehouse\php_rest_api\public\index.php on line 22
Routes.php
<?php
use Slim\Http\Request;
use Slim\Http\Response;
// Routes
$app->get('/[{name}]', function (Request $request, Response $response, array $args) {
$result = $this->course->getCourses();
return $response->withJson($result, 200, JSON_PRETTY_PRINT);
};
Course.php
<?php
namespace App\Model;
use App\Exception\ApiException;
class Course
{
protected $database;
public function __construct(\PDO $database)
{
$this->database = $database;
}
public function getCourses()
{
$statement = $this->database->prepare(
'SELECT * FROM courses ORDER BY id'
);
$statement->execute();
return $statement->fecthAll();
}
}
Dependencies.php
<?php
// DIC configuration
$container = $app->getContainer();
// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new \Slim\Views\PhpRenderer($settings['template_path']);
};
// monolog
$container['logger'] = function ($c) {
$settings = $c->get('settings')['logger'];
$logger = new \Monolog\Logger($settings['name']);
$logger->pushProcessor(new \Monolog\Processor\UidProcessor());
$logger->pushHandler(new \Monolog\Handler\StreamHandler($settings['path'], $settings['level']));
return $logger;
};
// API
$container['api'] = function($c) {
$api = $c->get('settings')['api'];
return $api;
};
$container['db'] = function($c) {
$db = $c->get('settings')['db'];
$pdo = new PDO($db['dsn'].':'.$db['database']);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
};
$container['course'] = function($c) {
return new App\Model\Course($c->get('db'));
};
index.php
<?php
if (PHP_SAPI == 'cli-server') {
// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
$dependencies = require __DIR__ . '/../src/dependencies.php';
*line 22 .....* $dependencies($app);
// Register middleware
$middleware = require __DIR__ . '/../src/middleware.php';
$middleware($app);
//Register routes
$routes = require __DIR__ . '/../src/routes.php';
*line..... 30* $routes($app);
// Run app
$app->run();
Jonathan Grieve
Treehouse Moderator 91,253 PointsHi Kennard,
I was able to reproduce this fix as well but I can't help but wonder if this is going to cause us problems again further down the line.
Can anyone confirm if this is the proper fix for the problem? The fact that we get a Fatal error: rather than a Slim Application Error indicates that we're using the right Namespace but I don't understand how commenting out those lines produces the required output because are those not what actives the routes and containers?
2 Answers
Alena Holligan
Treehouse TeacherYou index.php page should end like this
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
Jonathan Grieve
Treehouse Moderator 91,253 PointsThanks Alena, That should confirm to Kennard and I our code is fixed properly.
And I now know where the output in the browser is coming from. 😂
Kennard McGill
Full Stack JavaScript Techdegree Graduate 43,179 PointsCool Thanks Alena!
Kennard McGill
Full Stack JavaScript Techdegree Graduate 43,179 PointsKennard McGill
Full Stack JavaScript Techdegree Graduate 43,179 PointsSeems like this error goes away and the output shows if I comment out the dependencies and routes line below
Line 22 //$dependencies($app);
Line 30 //$routes($app);