苗条框架 - 找不到类“苗条\中间件”



我正在尝试创建一个自定义中间件类,但似乎无法调用extend SlimMiddleware,因为它找不到。

这是我的索引.php

<?php
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new SlimApp($settings);
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Run app
$app->run();

这是我的中间件.php

<?php
require_once("tester_auth.php");
$app->add(new TestAuth());

这是我的tester_auth.php,它是自定义中间件文件

<?php
use SlimMiddleware;
class TestAuth extends SlimMiddleware {
    public function __construct() {
        //Define the urls that you want to exclude from Authentication, aka public urls     
        $this->whiteList = array('/login');
    }
    public function deny_access() {
        $res = $this->app->response();
        $res->status(401);
    }
    public function isPublicUrl($url) {
        $patterns_flattened = implode('|', $this->whiteList);
        $matches = null;
        preg_match('/' . $patterns_flattened . '/', $url, $matches);
        return (count($matches) > 0);
    }
    public function call() {
        //Get the token sent from jquery
        $tokenAuth = $this->app->request->headers->get('Authorization');
        //We can check if the url requested is public or protected
        if ($this->isPublicUrl($this->app->request->getPathInfo())) {
            //if public, then we just call the next middleware and continue execution normally
            $this->next->call();
        } else {
            $this->deny_access(); 
        }
    }
}

它成功地到达了我的测试身份验证,但给了我错误Class 'Slim\Middleware' not found in /var/www/html/src/tester_auth.php on line 6......

编辑:

起初,我遇到了这个问题,因为我的 composer.json 没有安装中间件,但我已经更新了它,它现在就在那里,但仍然有同样的问题。

"require": {
        "php": ">=5.5.0",
        "slim/slim": "^3.8",
        "slim/php-view": "^2.0",
        "slim/middleware": "*",
        "monolog/monolog": "^1.17"
    }

尝试重新启动HTTP服务器(ngixn,PHP Web server,..(,以防它缓存一些PHP。

顺便说一句,苗条/中间件适用于苗条2。,但您使用的是 3..

最新更新