Welcome Guest, Not a member yet? Register   Sign In
re-route with optional argument. possible bug??
#1

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.
Harpreet Bhatia
Web Developer / Analyst
Technical Reviewer - "Codeigniter 2 Cookbook"

Reply
#2

(This post was last modified: 06-08-2016, 01:44 PM by cartalot.)

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"
Reply
#3

(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.
Harpreet Bhatia
Web Developer / Analyst
Technical Reviewer - "Codeigniter 2 Cookbook"

Reply
#4

(This post was last modified: 06-08-2016, 11:56 PM by arma7x.)

Which CI version?
Keep calm.
Reply
#5

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

3.0.6
Harpreet Bhatia
Web Developer / Analyst
Technical Reviewer - "Codeigniter 2 Cookbook"

Reply
#6

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" ;
Keep calm.
Reply
#7

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

(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.
Harpreet Bhatia
Web Developer / Analyst
Technical Reviewer - "Codeigniter 2 Cookbook"

Reply
#9

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

(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"?
Harpreet Bhatia
Web Developer / Analyst
Technical Reviewer - "Codeigniter 2 Cookbook"

Reply




Theme © iAndrew 2016 - Forum software by © MyBB