Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

route_to() function not work if greater than 3 parameters. #992

Closed
HieuPT7 opened this issue Apr 20, 2018 · 5 comments
Closed

route_to() function not work if greater than 3 parameters. #992

HieuPT7 opened this issue Apr 20, 2018 · 5 comments

Comments

@HieuPT7
Copy link
Contributor

HieuPT7 commented Apr 20, 2018

Hi
In file router.php

$routes->get('test/(:segment)/(:segment)', 'TestController::�index/$1/$2', ['as' => 'testRouter']);

In controller:
echo route_to('testRouter', 1, 2);
or use redirect()->route('testRouter', [1, 2]);

Expected result:
/test/1/2

Actual result:
/test/1/1

Thanks

@natanfelles
Copy link
Contributor

natanfelles commented Apr 20, 2018

The replacement is occurring because of the same pattern:

https://github.com/bcit-ci/CodeIgniter4/blob/74e95d35edf38b6d3244cf862186454a5df67fdb/system/Router/RouteCollection.php#L1132-L1135

Test:

$routes->get('test/(:any)/(:segment)', 'TestController::index/$1/$2', ['as' => 'testRouter']);

@natanfelles
Copy link
Contributor

Maybe preg_replace helps.

@natanfelles
Copy link
Contributor

This solves:

CodeIgniter4/system/Router/RouteCollection.php:1132

if (preg_match("|{$pattern}|", $params[$index]))
{
	$pos = strpos($from, $pattern);

	if ($pos !== false)
	{
		$from = substr_replace($from, $params[$index], $pos, strlen($pattern));
	}
}

@natanfelles
Copy link
Contributor

Parameters ordering continues working in Controllers:

$routes->get('test/(:segment)/(:segment)/(:segment)', 'TestController::test/$2/$1/$3', ['as' => 'testRouter']);

http://localhost:8080/test/1/2/3

public function test($param1, $param2, $param3)
{
	var_dump($param1);
	var_dump($param2);
	var_dump($param3);
}

@lonnieezell
Copy link
Member

@natanfelles Thanks for digging into this one! Fix being committed currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants