CodeIgniter Forums
Filters not working in CodeIgniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Filters not working in CodeIgniter 4 (/showthread.php?tid=79592)



Filters not working in CodeIgniter 4 - [email protected] - 07-06-2021

PHP Code:
filter
use Modules\Login\Filters\Auth;
use 
Modules\Login\Filters\Noauth;

public 
$aliases = [
"auth"    => Auth::class,
"noauth"    => Noauth::class,
];

Auth
public function before(RequestInterface $request$arguments null)
{
    if (!session()->get('isAdminLoggedIn')) {
        return redirect()->to(base_url(route_to('admin')));
    }
}

Route
$routes
->get("dashboard""DashboardController::index", ["filter" => "auth"]); 


What is the issue in above code. Filter is not working in CodeIgniter 4. Not working filter auth in dashboard route.


RE: Filters not working in CodeIgniter 4 - Magellan - 07-06-2021

The filter seems correct to me, maybe the problem is in the 'isAdminLoggedIn' session value,. Are you sure it is correctly set?


RE: Filters not working in CodeIgniter 4 - ikesela - 07-06-2021

try follow format must implement before and after function


RE: Filters not working in CodeIgniter 4 - paliz - 07-06-2021

PHP Code:
<?php

namespace Config;

use 
CodeIgniter\Config\BaseConfig;
use 
CodeIgniter\Filters\CSRF;
use 
CodeIgniter\Filters\DebugToolbar;
use 
CodeIgniter\Filters\Honeypot;
use 
Modules\Auth\Filters\AuthFilter;
use 
Modules\Auth\Filters\CsrfFilter;
use 
Modules\Auth\Filters\JwtFilter;
use 
Modules\Auth\Filters\ThrottleFilter;
use 
Modules\Common\Filters\ContentNegotiationFilter;
use 
Modules\Common\Filters\CorsFilter;
use 
Modules\Common\Filters\UrlFilter;

class 
Filters extends BaseConfig
{
    /**
    * Configures aliases for Filter classes to
    * make reading things nicer and simpler.
    *
    * @var array
    */
    public $aliases = [
        //'csrf'    => CSRF::class,
        'toolbar' => DebugToolbar::class,
        'honeypot' => Honeypot::class,
        'toolbar' => DebugToolbar::class,
        'honeypot' => Honeypot::class,
        'csrf' => CsrfFilter::class,
        'cors' => CorsFilter::class,
        'auth' => AuthFilter::class,
        'jwt' => JwtFilter::class,
        'url' => UrlFilter::class,
        'throttle' => ThrottleFilter::class,
        'contentNegotiation' => ContentNegotiationFilter::class,

    ];

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

    /**
    * List of filter aliases that works on a
    * particular HTTP method (GET, POST, etc.).
    *
    * Example:
    * 'post' => ['csrf', 'throttle']
    *
    * @var array
    */
    public $methods = [

        //  'get' => ['csrf'],
        // 'post' => ['csrf'],
        // 'put' => ['csrf'],
        // 'delete' => ['csrf']

    ];

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

    ];


PHP Code:
<?php namespace Modules\Common\Filters;

use 
Modules\Shared\Enums\FilterErrorType;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\Response;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\Filters\FilterInterface;
use  CodeIgniter\Config\Services;


class 
UrlFilter implements FilterInterface
{
    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {

    }

    public function before(RequestInterface $request$arguments null)
    {

        $response Services::response();
        $uri uri_string();
        $pathArray = array();
        if (strlen($uri)>1) {
            $pathArray explode('/'$uri);
        }

        if (count($pathArray) <= 1) {
            return $response->setJSON(['success' => false,
                'error' => lang('Site.filter.url'),
                'type' => FilterErrorType::Url])->setContentType('application/json')
                ->setStatusCode(Response::HTTP_BAD_REQUESTlang('Response.filter.url'));
        }

        if (in_array('api'$pathArray) === false){
            return $response->setJSON(['success' => false,
                'error' => lang('Site.filter.url'),
                'type' => FilterErrorType::Url])->setContentType('application/json')
                ->setStatusCode(Response::HTTP_BAD_REQUESTlang('Response.filter.url'));
        }


    }






RE: Filters not working in CodeIgniter 4 - [email protected] - 07-06-2021

(07-06-2021, 08:18 AM)Magellan Wrote: The filter seems correct to me, maybe the problem is in the 'isAdminLoggedIn' session value,. Are you sure it is correctly set?

Yes it is correctly set

(07-06-2021, 08:56 AM)ikesela Wrote: try follow format  must implement before and after function

Both functions before and after is already implemented but filters not working.


RE: Filters not working in CodeIgniter 4 - MGatner - 07-07-2021

What does not work? The filter is not applied? Or some error?


RE: Filters not working in CodeIgniter 4 - Chroma - 07-30-2021

I had a lot of trouble figuring out filters, but I see that you have api* rather than what I would have expected api/*

I don't know if that matters, but it might.

I am using filters in a custom namespace and that is working fine in all version of CI 4 that I have tried.