Welcome Guest, Not a member yet? Register   Sign In
Route passing $1 into the controller function instead of the value
#1

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.
Reply
#2

That should explain everything: https://codeigniter.com/user_guide/incom...ior-of-any
Reply
#3

(This post was last modified: Yesterday, 03:47 PM by Alex.Fry.)

(Yesterday, 12:16 AM)michalsn Wrote: That should explain everything: https://codeigniter.com/user_guide/incom...ior-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/incom...ior-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
Reply
#4

Are you working on the local or production server?

I guess, this might be helpful: https://codeigniter.com/user_guide/insta...g-htaccess
Reply
#5

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 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

(This post was last modified: 6 hours ago by davis.lasis.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB