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.
If you return a Responce instance in a before filter it will be sent back to the client and the script execution will stop. https://codeigniter.com/user_guide/incoming/filters.html#before-filters
It's normal then that your second filter doesn't run.
In general, avoid to return anything in a before filter if you want it to not stop your script.