CodeIgniter Forums
re-route with optional argument. possible bug?? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: re-route with optional argument. possible bug?? (/showthread.php?tid=65395)

Pages: 1 2


re-route with optional argument. possible bug?? - harpreet - 06-07-2016

hello there,
so i was working a project and tried to make an obvious route that did not work.
Example if you have a route like
PHP Code:
$route["admin/foos/(:num)"] = "admin/hello/$1"
and a function in admin controller as
PHP Code:
public function hello($arg=null) {
        if(empty(
$arg)){
            echo 
"is NULL";
        }
        else{
            echo 
"has $arg";
        }
    } 
1. The expected result on calling http://localhost/<project>admin/foos should be  (but is 404)
Code:
is NULL

2. calling url http://localhost/<project>/admin/foos/547 is as expected

Code:
has 547
Unexpectedly case 1 does not work, its lands to native CI 404 page. This only happens when you are making a route.
If I remove the route like
PHP Code:
// $route["admin/foos/(:num)"] = "admin/hello/$1"; 
and call url direclty  as http://localhost/<project>/admin/hello
the result is as expected
Code:
is NULL
http://localhost/<project>/admin/hello/586 is
PHP Code:
has 586 

The workaround i found was
PHP Code:
$route["admin/foos/?(:num)?"] = "admin/hello/$1"
Any help is appreciated.


RE: re-route with optional argument. possible bug?? - cartalot - 06-08-2016

Well i will leave it to smarter people then me whether this is a "bug" or not. But for me it is exactly what i would expect. If i'm assigning a route, and that route is supposed to have a number, then if the url does not have a number it should be a 404.

If however its a case of - it could have a number - or not - then assign a route for the other case. Then it will work for either condition:
PHP Code:
$route["admin/foos/(:num)"] = "admin/hello/$1"
$route["admin/foos"] = "admin/hello"



RE: re-route with optional argument. possible bug?? - harpreet - 06-08-2016

(06-08-2016, 01:43 PM)cartalot Wrote: Well i will leave it to smarter people then me whether this is a "bug" or not. But for me it is exactly what i would expect. If i'm assigning a route, and that route is supposed to have a number, then if the url does not have a number it should be a 404.

If however its a case of - it could have a number - or not - then assign a route for the other case. Then it will work for either condition:
PHP Code:
$route["admin/foos/(:num)"] = "admin/hello/$1"
$route["admin/foos"] = "admin/hello"

yes this too can work. agreed. But i would like to know if what i reported can be done.


RE: re-route with optional argument. possible bug?? - arma7x - 06-08-2016

Which CI version?


RE: re-route with optional argument. possible bug?? - harpreet - 06-09-2016

(06-08-2016, 11:56 PM)arma7x Wrote: Which CI version?

3.0.6


RE: re-route with optional argument. possible bug?? - arma7x - 06-09-2016

http://localhost/<project>admin/foos does link to $route ["admin/foos" ] = "admin/hello" ;(which is not define in route) and i think will not link to
$route ["admin/foos/(:num)" ] = "administered/hello/$1" ;


RE: re-route with optional argument. possible bug?? - arma7x - 06-09-2016

$route ["admin/foos" ] = "admin/hello" ; is not define. http://localhost/<project>admin/foos and http://localhost/<project>admin/foos/bars is different.


RE: re-route with optional argument. possible bug?? - harpreet - 06-09-2016

(06-09-2016, 01:09 AM)arma7x Wrote: http://localhost/<project>admin/foos does link to $route ["admin/foos" ] = "admin/hello" ;(which is not define in route) and i think will not link to
$route ["admin/foos/(:num)" ] = "administered/hello/$1" ;

The problem is that even though the parameter is optional, the route
PHP Code:
$route["admin/foos/(:num)"] = "admin/hello/$1"
should still behave as
PHP Code:
$route["admin/hello/(:num)"] = "admin/hello/$1"
simply calling urls 
Code:
http://localhost/<project>admin/foos
and
Code:
http://localhost/<project>admin/hello
should show same result.


RE: re-route with optional argument. possible bug?? - ciadmin - 06-09-2016

The parameter in your rule is not optional. You need to do what cartalot suggested in the first reply to your post.


RE: re-route with optional argument. possible bug?? - harpreet - 06-09-2016

(06-09-2016, 02:10 AM)ciadmin Wrote: The parameter in your rule is not optional. You need to do what cartalot suggested in the first reply to your post.

So should we consider this "missing feature" or "bug"?