Codeigniter 4过滤器不起作用

问题描述

嗨,我想创建自己的授权来研究框架的新版本... 这是我的路线:


$routes->add('/user/login','User::login',['filter'=>'usersFiltersNoAuth']);
$routes->add('/login',['filter'=>'usersFiltersNoAuth']);
$routes->add('/user/registration','User::registration',['filter'=>'usersFiltersNoAuth']);
$routes->add('/logout','User::logout');
$routes->add('/user/changeEmail','User::changeEmail',['filter'=>'usersFiltersAuth']);
$routes->add('/user/changePassword','User::changePassword',['filter'=>'usersFiltersAuth']); 

这是我的2个过滤器类:

class UsersFiltersNoAuth implements FilterInterface
{
    /**
     * Do whatever processing this filter needs to do.
     * By default it should not return anything during
     * normal execution. However,when an abnormal state
     * is found,it should return an instance of
     * CodeIgniter\HTTP\Response. If it does,script
     * execution will end and that Response will be
     * sent back to the client,allowing for error pages,* redirects,etc.
     *
     * @param \CodeIgniter\HTTP\RequestInterface $request
     * @param array|null                         $params
     *
     * @return mixed
     */
    public function before(RequestInterface $request,$params = null)
    {
        // if no user is logged in then send them to the login form
        if (isset($_SESSION['user_id']))
        {
                        
            return redirect()->to('/user/index');
        }
    }

    //--------------------------------------------------------------------

    /**
     * Allows After filters to inspect and modify the response
     * object as needed. This method does not allow any way
     * to stop execution of other after filters,short of
     * throwing an Exception or Error.
     *
     * @param \CodeIgniter\HTTP\RequestInterface  $request
     * @param \CodeIgniter\HTTP\ResponseInterface $response
     * @param array|null                          $arguments
     *
     * @return void
     */
    public function after(RequestInterface $request,ResponseInterface $response,$arguments = null)
    {

    }

    //--------------------------------------------------------------------

}   // End of UsersFiltersNoAuth Class. 

class UsersFiltersAuth implements FilterInterface
{
    /**
     * Do whatever processing this filter needs to do.
     * By default it should not return anything during
     * normal execution. However,$params = null)
    {
        // if no user is logged in then send them to the login form
        if (!isset($_SESSION['user_id']))
        {
            
            session()->set('redirect_url',current_url());
            
            return redirect()->to('/login');
        }
    }

    //--------------------------------------------------------------------

    /**
     * Allows After filters to inspect and modify the response
     * object as needed. This method does not allow any way
     * to stop execution of other after filters,$arguments = null)
    {

    }

    //--------------------------------------------------------------------

}   // End of UsersFiltersAuth Class. 

如果(设置了$ _SESSION ['user_id])时我尝试转到/ user / chengeEmail或/ user / changePassword,为什么我重定向到/ user / index?

此外,有一种方法可以将过滤器应用于整个控制器?除了某些方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)