简体   繁体   中英

Routing issues with custom route in Codeigniter

My controller structure (api is the folder inside controller)

controllers/api/Api_1_0.php
controllers/api/Api_2_0.php

In my routes.php

$route['api/(\d+)\.(\d+)']          = "api/Api_$1_$2";
$route['api/(\d+)\.(\d+)/(:any)']   = "api/Api_$1_$2/$3";

The routing strategy i need is, if /api/2.0 is specified it will point to controller file Api_2_0.php

ie. api/ x . y points to file Api_ x _ y .php

Everything working fine with above routing but below is my issue:

https://www.example.com/api/2.0/photos/1234567890 // not working

https://www.example.com/api/2.0/photos // working

How to solve ?

As mentioned in a comment you should change the order.

But you should also change (:any) to (.*). (:any) would only match first segment of your url.

So correct way would be:

$route['api/(\d+)\.(\d+)/(.*)']   = "api/api_$1_$2/$3";
$route['api/(\d+)\.(\d+)']        = "api/api_$1_$2";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM