John Davidson

php - Is it possible to apply multiple filters to a single route in CodeIgniter 4

0 comments
Message:


I have tried several ways to achieve this, but none seems to work,
it seems CodeIgniter 4 does not have the ability to apply multiple filters to a single route, currently here is what I am trying:


ProvideInfoFilter.php:


<?php namespace App\Filters;

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

class ProvideinfoFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
echo "pinfo";
$account_data = new \App\Libraries\Account_Data;

return $account_data->no_info_redirect();
}

public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{

}
}

AccessFilter.php:


<?php namespace App\Filters;

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

class AccessFilter implements FilterInterface
{
public function before(RequestInterface $request, $arguments = null)
{
$account_data = new \App\Libraries\Account_Data;

echo "accessf";
if ($request->uri->getSegment(1) !== 'm' && $request->uri->getSegment(2) !== 'm' && !$request->getGet('token'))
{
return $account_data->is_logged_in();
}
}

public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{

}
}

Filters.php:


<?php namespace Config;

use CodeIgniter\Config\BaseConfig;

class Filters extends BaseConfig
{
public $aliases = [
'toolbar' => \CodeIgniter\Filters\DebugToolbar::class,
'accesscontrol' => \App\Filters\AccessFilter::class,
'provide_info' => \App\Filters\ProvideinfoFilter::class
];

public $globals = [
'before' => [
],
'after' => [
'toolbar'
],
];

public $methods = [];

public $filters = [
'provide_info' => ['before' => ['user', 'user/*']],
'accesscontrol' => ['before' => ['user', 'user/*']]
];
}

I have added echo statements for debugging


The problem is, Commenting out either 'accesscontrol' => ['before' => ['user', 'user/*']] or 'provide_info' => ['before' => ['user', 'user/*']] applies either of the filters the and the echoed string can be seen in the output. But having both of them like demonstrated above does not apply both of them.


It is important for me to have both filters running as I want to apply specific exemptions for each of them using the $globals array.



My Answer:
php

namespace App\Filters;

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

class ProvideInfoFilter implements FilterInterface
{
public function before(RequestInterface $request)
{
// Logic before applying filter
return $request;
}

public function after(RequestInterface $request, ResponseInterface $response)
{
// Logic after applying filter
return $response;
}
}


php

namespace App\Filters;

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

class AnotherFilter implements FilterInterface
{
public function before(RequestInterface $request)
{
// Logic before applying filter
return $request;
}

public function after(RequestInterface $request, ResponseInterface $response)
{
// Logic after applying filter
return $response;
}
}


In your routes file, you can apply multiple filters to a single route like this:

php
$routes->get('example', 'ExampleController::index', ['filter' => 'ProvideInfoFilter|AnotherFilter']);


This way, both `ProvideInfoFilter` and `AnotherFilter` will be applied to the `example` route.

Rate this post

4 of 5 based on 5627 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us