11

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?

Community
  • 1
  • 1
Kelly Keller-Heikkila
  • 2,544
  • 6
  • 23
  • 35
  • 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 Oct 19 '15 at 15:01
  • Also, if you're importing the class via *use* don't put the namespace when creating a new instance – mTorres Oct 19 '15 at 15:03

1 Answers1

22

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();
mTorres
  • 3,590
  • 2
  • 25
  • 36
  • For me it did not help :( Same error (`Fatal error: Class 'App' not found in...`) – Vlada Katlinskaya Feb 27 '17 at 19:12
  • 2
    @VladaKatlinskaya, Did you put the use \Slim\App sentence before instatiating the application variable? If you did so, just ask another question mentioning that this solution didn't work for you. – mTorres Mar 04 '17 at 11:12