简体   繁体   English

slim 3中间件和psr-4 composer自动加载器:找不到类

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

composer.json : composer.json

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

src/Auth/RequestMethodPathRule.php: src / Auth / RequestMethodPathRule.php:

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

src/middleware.php src / middleware.php

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

This results in 这导致

Class 'App\\Auth\\RequestMethodPathRule' not found. 找不到类“ App \\ Auth \\ RequestMethodPathRule”。

Including all of the code in RequestMethodPathRule.php in the file middleware.php works fine. 在文件Middleware.php中将RequestMethodPathRule.php中的所有代码都包含在内可以正常工作。

More strangely, along with the 500 error, the browser displays the source code of RequestMethodPathRule.php! 更奇怪的是,随着500错误,浏览器显示RequestMethodPathRule.php的源代码!

EDIT: Oh my, I had a shorttag on the class file (<? instead of <?php) so php treated it as text. 编辑:哦,我的,我在类文件上有一个短标记(<? instead of <?php)因此php将其视为文本。 Habits are hard to quit. 习惯很难戒掉。

More strangely, along with the 500 error, the browser displays the source code of RequestMethodPathRule.php! 更奇怪的是,随着500错误,浏览器显示RequestMethodPathRule.php的源代码!

That's mean that composer correctly include the file, but no class is found because the code isn't interpret by php. 这意味着作曲家可以正确包含文件,但是找不到类,因为该代码无法由php解释。 In this case, since the short tags aren't a usually enabled, you just need to replace them with the <?php tag instead. 在这种情况下,由于通常不启用short标记,因此您只需要将它们替换为<?php标记即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM