首页 > 解决方案 > 过滤器在 Codeiginter 4 中不起作用

问题描述

我正在使用框架 Codeiginter 4(学习)。我正在使用过滤器来验证会话,但过滤器不起作用。你能帮助如何在 CI4 中使用过滤器。如果我没有登录需要加载登录页面......无需重定向到那个特定的控制器。

我的过滤器代码:

<?php

namespace App\Filters;

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

class Auth implements FilterInterface
{
    public function before(RequestInterface $request, $arguments = null)
    {
         $session = session();
         
        if(!$session->has('logged_user')){

            return redirect()->to('/Login'); 
        }
    }

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

在配置\过滤器

public $aliases = [
        'csrf'     => CSRF::class,
        'toolbar'  => DebugToolbar::class,
        'honeypot' => Honeypot::class,
        'auth' => \App\Filters\Auth::class,
        'noauth' => \App\Filters\NoAuth::class,

    ];

路由.php

$routes->get('dashboard','Dashboard::index',['filter' =>'auth']);
$routes->get('role/index','Role::index',['filter' =>'auth']);

请帮我解决这个问题。我很累但不为我工作

标签: codeigniter-4

解决方案


推荐阅读