10
votes

I want to use Slim 3 in a subdirectory, but cannot seem to load it. All files are contained in the subdirectory, including composer.json. Here is my composer.json:

"require": {
    "slim/slim": "3.0.0-RC1"
}

Here is my script:

<?php
require "vendor/autoload.php";
use \Slim\Slim;

$app = new \Slim\Slim();
$app->get('/subdirectory/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();

I tried many things, including Class Slim not found when installing slim with composer and PHP Fatal error: Class 'Slim' not found. Unfortunately, they didn't solve my problem.

The error I get is Fatal error: Class 'Slim\Slim' not found in ... on line 5, which corresponds to $app = new \Slim\Slim();.

Anyone know what I'm missing?

1
Have you checked if there are open issues with that RC1? Have you checked if the autoloader is correctly included? Have you checked what path the autoloader uses? Have you tried to dump autoloader? - m02ph3u5
Also, if you're importing the class via use don't put the namespace when creating a new instance - mTorres

1 Answers

21
votes

It seems that Slim3 is not using Slim as main class name but App.

So your code should be:

<?php
require "vendor/autoload.php";
use \Slim\App;

$app = new App();
$app->get('/subdirectory/hello/:name', function ($name) {
    echo "Hello, $name";
});
$app->run();