CodeIgniter Forums
Route passing $1 into the controller function instead of the value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Route passing $1 into the controller function instead of the value (/showthread.php?tid=92943)



Route passing $1 into the controller function instead of the value - Alex.Fry - 05-22-2025

Hi team,
I have a new issue that is confusing me 
Using CodeIgnighter4, I have routes set up :
$routes->get("Entry/(:any)",'Home::Entry/$1');
$routes->get("LoadEvents/(:any)",'Home::Load_Events/$l/$2');

In the Controller:
    public function Entry($Hive_ID,$Entry_ID){ blah blah....}
    public function Load_Events($BeeKeeper_ID,$mth){
        echo "$BeeKeeper_ID<br>";
        echo "$mth<br>";


The Entry route works without issue... (tested from the application and from postman)
The Load_Events route echoes $1 as $BeeKeeper and nothing for $mth

Can anyone shed some light on this behaviour ? 

I have tried 
$routes->get("LoadEvents/(:any)",'Home::Load_Events/$l'); 
$routes->get("LoadEvents/(:any)/(:any)",'Home::Load_Events/$l/$2');
as well to no avail...


Any assistance will be appreciated.


RE: Route passing $1 into the controller function instead of the value - michalsn - 05-22-2025

That should explain everything: https://codeigniter.com/user_guide/incoming/routing.html#the-behavior-of-any


RE: Route passing $1 into the controller function instead of the value - Alex.Fry - 05-22-2025

(Yesterday, 12:16 AM)michalsn Wrote: That should explain everything: https://codeigniter.com/user_guide/incoming/routing.html#the-behavior-of-any

Thanks ,yes I've been through the document... according to the doc...

$routes->get("LoadEvents/(:any)",'Home::LoadEvents/$l');

Should send a URI like "www.myapp.com/LoadEvents/222/333 through to Home::LoadEvents($P1,$P1).... with $P1 = 222 and $P2 = 333..... This happens correctly with other routes entries as mentioned.

Except that the observed behaviour is 
"www.myapp.com/LoadEvents/222/333 FAILS with 2 parameters expected, 1 passed.

Changing to 
$routes->get("LoadEvents/(:any)",'Home::LoadEvents/$l/$2);
results in 
www.myapp.com/LoadEvents/222/333 through to Home::LoadEvents($P1,$P1).... with $P1 = $1 and $P2 = nothing


The default behaviour set by routing.php
public bool $multipleSegmentsOneParam = false;

(Yesterday, 03:42 PM)Alex.Fry Wrote:
(Yesterday, 12:16 AM)michalsn Wrote: That should explain everything: https://codeigniter.com/user_guide/incoming/routing.html#the-behavior-of-any

Thanks ,yes I've been through the document... according to the doc...

$routes->get("LoadEvents/(:any)",'Home::LoadEvents/$l');

Should send a URI like "www.myapp.com/LoadEvents/222/333 through to Home::LoadEvents($P1,$P1).... with $P1 = 222 and $P2 = 333..... This happens correctly with other routes entries as mentioned.

Except that the observed behaviour is 
"www.myapp.com/LoadEvents/222/333 FAILS with 2 parameters expected, 1 passed.

Changing to 
$routes->get("LoadEvents/(:any)",'Home::LoadEvents/$l/$2);
results in 
www.myapp.com/LoadEvents/222/333 through to Home::LoadEvents($P1,$P1).... with $P1 = $1 and $P2 = nothing


The default behaviour set by routing.php
public bool $multipleSegmentsOneParam = false;

Hmmm, ok, even more confusing www.myapp.com/public/index.php/LoadEvents/222/333 works as expected.... This feels like a htaccess issue


RE: Route passing $1 into the controller function instead of the value - michalsn - 05-22-2025

Are you working on the local or production server?

I guess, this might be helpful: https://codeigniter.com/user_guide/installation/deployment.html#adding-htaccess


RE: Route passing $1 into the controller function instead of the value - InsiteFX - 05-22-2025

PHP Code:
$routes->get("Entry/(:any)",'Home::Entry/$1');
$routes->get("LoadEvents/(:any)",'Home::Load_Events/$l/$2'); <-- looks like an l not a 1 



RE: Route passing $1 into the controller function instead of the value - davis.lasis - 05-22-2025

Routes are most flexible thing i have ever used

PHP 8.3
CI 4.6.0

PHP Code:
$routes->get('{locale}/LoadEvents/(:any)', [FrontendController::class, 'LoadEvents/$1']);
$routes->get('{locale}/LoadEvents2/(:any)', [FrontendController::class, 'LoadEvents2']); 




PHP Code:
public function LoadEvents($id_1$id_2): string
    
{
        d($id_1);
        dd($id_2);
    }

    public function LoadEvents2(...$params): string
    
{
        dd($params);
    




1. www.domain.com/en/LoadEvents/123/987

PHP Code:
$id_1 string (3"123" 
PHP Code:
$id_2 string (3"987" 


2. www.domain.com/en/LoadEvents2/123/987/456/beekeeper/mnt-top

PHP Code:
$params array (5)
=> string (3"123"
=> string (3"987"
=> string (3"456"
=> string (9"beekeeper"
=> string (7"mnt-top" 

(7 hours ago)InsiteFX Wrote:
PHP Code:
$routes->get("Entry/(:any)",'Home::Entry/$1');
$routes->get("LoadEvents/(:any)",'Home::Load_Events/$l/$2'); <-- looks like an l not a 1 

Correct, that was small L, not integer 1