Hello,
I used Grocery CRUD with CI3 for many years, but recently I started to use CI4 and I would love to keep using Grocery CRUD there as well
I have an issue when the form is submitted, I get the error message: âThe action you requested is not allowed.â which is shown when the CSRF validation doesnât work.
In the documentation I found the two functions setCsrfTokenName and setCsrfTokenValue, but they are not found in my crud class, so I guess that they are only for the Enterprise version, while Iâm using the community version.
So Iâm wondering whatâs the best way to disable CSRF only for specific routes (in my case, all the admin ones). I know that I need to do in the filters config file, but I really didnât understand where I should do it.
Here you can find a copy of my Config/Filters.php file.
From CI4 documentation I know that I can enable CSRF everywhere except for some pages (i.e. all the ones that have an url like âadmin/*â), but I didnât understand in which array I should write it and how (I tried several things that didnât work).
Thank you in advance for your help!
<?php
namespace Config;
use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Filters\CSRF;
use CodeIgniter\Filters\DebugToolbar;
use CodeIgniter\Filters\Honeypot;
use CodeIgniter\Filters\InvalidChars;
use CodeIgniter\Filters\SecureHeaders;
class Filters extends BaseConfig
{
/**
* Configures aliases for Filter classes to
* make reading things nicer and simpler.
*
* @var array<string, class-string|list<class-string>> [filter_name => classname]
* or [filter_name => [classname1, classname2, ...]]
*/
public array $aliases = [
'csrf' => CSRF::class,
'toolbar' => DebugToolbar::class,
'honeypot' => Honeypot::class,
'invalidchars' => InvalidChars::class,
'secureheaders' => SecureHeaders::class,
];
/**
* List of filter aliases that are always
* applied before and after every request.
*
* @var array<string, array<string, array<string, string>>>|array<string, list<string>>
*/
public array $globals = [
'before' => [
// 'honeypot',
// 'csrf',
// 'invalidchars',
],
'after' => [
'toolbar',
// 'honeypot',
// 'secureheaders',
],
];
/**
* List of filter aliases that works on a
* particular HTTP method (GET, POST, etc.).
*
* Example:
* 'post' => ['foo', 'bar']
*
* If you use this, you should disable auto-routing because auto-routing
* permits any HTTP method to access a controller. Accessing the controller
* with a method you don't expect could bypass the filter.
*/
public array $methods = [
'post' => [
'csrf'
]
];
/**
* List of filter aliases that should run on any
* before or after URI patterns.
*
* Example:
* 'isLoggedIn' => ['before' => ['account/*', 'profiles/*']]
*/
public array $filters = [];
}