codeigniter 代码点火器4错误:未找到类“应用程序\筛选器\筛选器Jwt”

csbfibhn  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(124)

我尝试用CI4创建一个JWT过滤器,但是当我在app/Config/Filter.php文件中设置我的过滤器时,它总是抛出一个错误,尽管它确实在app/Filters/目录中。

Class "App\Filters\FilterJwt" not found

我一直试图在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
    }
}
    • 文件在目录中的证明:**

    • CI4错误:**

如您所见,CI4框架找不到"Filters"文件夹,尽管它就在那里。

mjqavswn

mjqavswn1#

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

代替:

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

class MyFilter implements FilterInterface
{
//...

使用这个:

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

class FilterJwt implements FilterInterface
{
//...

相关问题