CodeIgniter4 icon indicating copy to clipboard operation
CodeIgniter4 copied to clipboard

Missing Feature, routes are empty when trying route_to in filters

Open crustamet opened this issue 1 year ago • 1 comments

I do not know if this is a wanted behaviour or not

But having this Filter Config for example

	public array $globals = [
		'before' => [
			'honeypot' => ['except' => [route_to('some_alias')]],
			// 'csrf',
			// 'invalidchars',
		],
		'after' => [
			// 'honeypot',
			// 'secureheaders',
		],
	];

first it does not allow you to have it as a constant with dynamic strings

so i change the filter config with this addition instead of putting in the globals

	public function __construct()
	{
		$routes = Services::routes();
		
		echo route_to('some_alias'); 

		$this->globals['before']['honeypot']['except'] = [route_to('route_alias')];
	}

It does not work because routes are not loaded yet from the Codeigniter->Run() method because of this piece of code here https://github.com/codeigniter4/CodeIgniter4/blob/develop/system/CodeIgniter.php#L346

The handling of the request happens after the filters were loaded....

The filters are loaded first then the routes configuration file ... why is that ? I thought that routes were important without them nothing works, i know there is an AutoRouteImprove feature or what not. even though routes should be already loaded here no ?

I just want to access my routes that i have defined in the filters configuration file...

Is there a way to do this ?

crustamet avatar Dec 17 '24 12:12 crustamet

i have moved the handling of the request before the filters are loaded and for sure now i can see my defined routes from my routes config file...

I'm sure that this is not the right approach..

crustamet avatar Dec 17 '24 12:12 crustamet

CI4 has internal limitations. Especially in the configuration area, as classes are used. I think it's necessary to accept this.

See #9231 Your changes may cause errors and are dangerous. This is unlikely to be resolved in the near future.

neznaika0 avatar Sep 07 '25 16:09 neznaika0

yes i know i witnessed everything :) i tried but everything was getting worse and worse...

I actually filtered the routes inside the filter itself and use it like that and return nothing on that route.. to exclude it

	$router = Services::router();
	$routes = Services::routes();
	
	$exceptRoutes = [
		// 'homepage',
		'cookie',
		'test',
	];
	
	$namedRoutes = $router->getMatchedRouteOptions();
	
	if(isset($namedRoutes['as']) AND !empty($namedRoutes['as']))
	{
		if(in_array($namedRoutes['as'], $exceptRoutes))
		{
			return;
		}
	}

crustamet avatar Sep 10 '25 16:09 crustamet