Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

Missing argument in route when using `url_to` or `route_to` with regex routes

Abierto
#9,250 5 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
48/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
php
Área
backend

Línea de trabajo

Reproduzca el fallo utilizando las rutas de Routes.php y compare url_to y route_to con ambas configuraciones de multipleSegmentsOneParam en Routing.php. Comience rastreando el manejo de los parámetros de ruta desde las rutas regex con nombre; el trabajo estará terminado cuando todos los enlaces mostrados se generen sin InvalidArgumentException y conserven los parámetros esperados.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

PHP Version

8.3

CodeIgniter4 Version

CodeIgniter 4.5.5

CodeIgniter4 Installation Method

Composer (using codeigniter4/appstarter)

Which operating systems have you tested for this bug?

Debian 12

Which server did you use?

fpm-fcgi

Database

N/A

What happened?

The following route definition works fine as long as you don't use url_to or route_to to refer to it:

$routes->get('/test(/(:any))?', 'Test::direct/$1', ['as' => 'test']);

Linking as below works:

<a href="/test">No parameter</a>
<a href="/test/param1">1 parameter</a>
<a href="/test/param1/param2">2 parameters</a>

But using both url_to or route_to fails:

<a href="<?= url_to('test') ?>">No parameter</a>
<a href="<?= url_to('test', 'param1') ?>">1 parameter</a>
<a href="<?= url_to('test', 'param1', 'param2') ?>">2 parameters</a>
Steps to Reproduce
  1. Add the new routes in Routes.php:
$routes->get('/test(/(:any))?', 'Test::direct/$1', ['as' => 'test']);
$routes->get('/test_urlto(/(:any))?', 'Test::urlto/$1', ['as' => 'urlto']);
$routes->get('/test_routeto(/(:any))?', 'Test::routeto/$1', ['as' => 'routeto']);
  1. Create the Test controller:
<?php

namespace App\Controllers;

class Test extends BaseController
{
    public function direct(...$params): string
    {
        return view('test', ['params' => $params]);
    }

    public function urlto(...$params): string
    {
        return view('test_urlto', ['params' => $params]);
    }

    public function routeto(...$params): string
    {
        return view('test_routeto', ['params' => $params]);
    }
}
  1. Create the respective views:

A) direct view

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Test</title>
</head>
<body>
	<h1>Testing CI4 Routing with Parameters</h1>

	<h2>Test using string directly</h2>
	<ul>
		<li>
			<a href="/test">No parameter</a>
		</li>
		<li>
			<a href="/test/param1">1 parameter</a>
		</li>
		<li>
			<a href="/test/param1/param2">2 parameters</a>
		</li>
		<li>
			<a href="/test/param1/param2/param3">3 parameters</a>
		</li>
	</ul>
	<h3>Passed Parameters</h3>
	<ul>
		<?php foreach ($params as $param) : ?>
			<li><?= $param ?></li>
		<?php endforeach; ?>
</body>
</html>

B) view using url_to

<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<title>Test</title>
</head>

<body>
	<h1>Testing CI4 Routing with Parameters</h1>
	<h2>Test using <code>url_to</code></h2>
	<ul>
		<li>
			<a href="<?= url_to('urlto') ?>">No parameter</a>
		</li>
		<li>
			<a href="<?= url_to('urlto', 'param1') ?>">1 parameter</a>
		</li>
		<li>
			<a href="<?= url_to('urlto', 'param1', 'param2') ?>">2 parameters</a>
		</li>
		<li>
			<a href="<?= url_to('urlto', 'param1', 'param2', 'param3') ?>">3 parameters</a>
		</li>
	</ul>

	<h3>Passed Parameters</h3>
	<ul>
		<?php foreach ($params as $param) : ?>
			<li><?= $param ?></li>
		<?php endforeach; ?>
</body>
</html>
  1. Access those views at: /test and /test_urlto
Expected Output

No InvalidArgumentException, Missing argument for "(/(:any)" in route "test_urlto(/(:any))?" while using both url_to and route_to .

Anything else?
  • One can try it with public bool $multipleSegmentsOneParam = true; or public bool $multipleSegmentsOneParam = false; in Routing.php. The error is the same in both situations (just the parameters are treated differently, as expected).

  • One can also use a regex like '(/(.+))?' instead of (/(:any))? . The error remains.

Lenguaje dominante
PHP
Estrellas
6k
Forks
2k
Merge medio
1 d 22 h
PR fusionados (30 d)
59

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de codeigniter4/CodeIgniter4

Todos los issues de codeigniter4/CodeIgniter4

Issues similares

Más issues de PHP

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.