无法访问App\Filters目录CodeIgniter4

pxyaymoc  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(102)

我尝试使用CI 4创建一个JWT过滤器,但是当我在/Config/Filter.php中设置过滤器时,它总是说没有找到该类,尽管它确实在/App/Filters/目录中。我一直尝试在/App中创建新目录,并将其命名为AuthFilter,但仍然没有成功

/配置/过滤器.php

<?php

namespace Config;

use App\Filters\FilterJwt;
use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Filters\CSRF;
use CodeIgniter\Filters\DebugToolbar;
use CodeIgniter\Filters\Honeypot;
use CodeIgniter\Filters\InvalidChars;
use CodeIgniter\Filters\SecureHeaders;

class Filters extends BaseConfig
{
    /**
     * Configures aliases for Filter classes to
     * make reading things nicer and simpler.
     */
    public array $aliases = [
        'csrf'          => CSRF::class,
        'toolbar'       => DebugToolbar::class,
        'honeypot'      => Honeypot::class,
        'invalidchars'  => InvalidChars::class,
        'secureheaders' => SecureHeaders::class,
        'authentification' => FilterJwt::class

    ];

    /**
     * List of filter aliases that are always
     * applied before and after every request.
     */
    public array $globals = [
        'before' => [
            // 'honeypot',
            // 'csrf',
            // 'invalidchars',
        ],
        'after' => [
            'toolbar',
            // 'honeypot',
            // 'secureheaders',
        ],
    ];

    /**
     * List of filter aliases that works on a
     * particular HTTP method (GET, POST, etc.).
     *
     * Example:
     * 'post' => ['foo', 'bar']
     *
     * If you use this, you should disable auto-routing because auto-routing
     * permits any HTTP method to access a controller. Accessing the controller
     * with a method you don’t expect could bypass the filter.
     */
    public array $methods = [];

    /**
     * List of filter aliases that should run on any
     * before or after URI patterns.
     *
     * Example:
     * 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
     * @var array
     */
    public $filters = [
        'authentification' => [
            'before' => [
                'users/*',
                'users'
            ]
        ]
    ];
}

/应用程序/过滤器/过滤器Jwt.php:

<?php

namespace App\Filters;

use CodeIgniter\Filters\FilterInterface;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;

use CodeIgniter\API\ResponseTrait;
use Exeption;

class MyFilter implements FilterInterface
{
    use ResponseTrait;
    public function before(RequestInterface $request, $arguments = null)
    {
        $header = $request->getServer('HTTP_AUTHORIZATION');
        try {
            helper('jwt');
            $encodedToken = getJwt($header);
            checkJWT($encodedToken);
            return $request;
        } catch (Exeption $e) {
            return Services::response()->setJson([
                'error' => $e->getMessage()
            ])->setStatusCode(ResponseInterface::HTTP_UNAUTHORIZED);
        }
    }

    public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
    {
        // Do something here
    }
}

文件位于目录中的证明:

CI 4错误:

正如你所看到的,CI 4找不到“过滤器”文件夹,虽然它就在那里。

4dbbbstv

4dbbbstv1#

  • 将类从“MyFilter”重命名为“FilterJwt”。*

代替:

/应用程序/过滤器/过滤器Jwt.php

class MyFilter implements FilterInterface
{
//...

使用这个:

/应用程序/过滤器/过滤器Jwt.php

class FilterJwt implements FilterInterface
{
//...

相关问题