slim 3 middleware & psr-4 composer autoloader: class not found

437 views Asked by At

composer.json:

"autoload": {
    "psr-4": {
        "App\\": "src/"
    }
},

src/Auth/RequestMethodPathRule.php:

namespace App\Auth;
class RequestMethodPathRule implements  \Slim\Middleware\HttpBasicAuthentication\RuleInterface 
{ 
...

src/middleware.php

$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "rules" => [new App\Auth\RequestMethodPathRule(

This results in

Class 'App\Auth\RequestMethodPathRule' not found.

Including all of the code in RequestMethodPathRule.php in the file middleware.php works fine.

More strangely, along with the 500 error, the browser displays the source code of RequestMethodPathRule.php!

EDIT: Oh my, I had a shorttag on the class file (<? instead of <?php) so php treated it as text. Habits are hard to quit.

1

There are 1 answers

0
Federkun On BEST ANSWER

More strangely, along with the 500 error, the browser displays the source code of RequestMethodPathRule.php!

That's mean that composer correctly include the file, but no class is found because the code isn't interpret by php. In this case, since the short tags aren't a usually enabled, you just need to replace them with the <?php tag instead.