Ticket #496 (closed Bug: fixed)

Opened 6 months ago

Last modified 6 months ago

Router prevents optional arguments from getting set to their default values.

Reported by: atomless Owned by: Geert
Priority: critical Milestone: 2.1.2
Component: Libraries Version: SVN HEAD
Keywords: Router arguments Cc:

Description

in short: the kohana router prevents optional arguments from getting set to their default values.



in detail:

If I add a route to my application routes config like so:



$config['testmethod'] = 'maincontroller/testmethod/';



as expected the url http://mydomain.com/testmethod is successfully routed to the testmethod within maincontroller -

if I also add this to the routes config:



$config['testmethod/(.+)'] = 'maincontroller/testmethod/$1';



then arguments passed to the testmethod via the url like so: http://mydomain.com/testmethod/arg1/arg2 are also successfully passed as arguments to the testmethod within maincontroller.

However, if the test method has a default setting for an optional argument like so:



public function testmethod($optionalarg1='foo')



optionalarg1 does not get set to the default value when the url has no arguments

optionalarg1 will always simply equal FALSE with the url http://mydomain.com/testmethod/

whereas bypassing the router like so http://mydomain.com/maincontroller/testmethod/ optionalarg1 will equal 'foo'

Change History

Changed 6 months ago by atomless

  • priority changed from major to critical

Changed 6 months ago by kolanos

Proposed fix:

system/libraries/Router.php line 27:

public static $arguments = FALSE;

changed to

public static $arguments = NULL;

Changed 6 months ago by Geert

  • owner changed from - No owner - to Geert
  • status changed from new to assigned

Took me quite some time to debug this one, but I found out what is going wrong, atomless. The problem is caused by your first route:

$config['testmethod'] = 'maincontroller/testmethod/';

Because you add a slash at the end, you are in fact supplying an empty argument. Remove it:

$config['testmethod'] = 'maincontroller/testmethod';

I guess Kohana should automatically trim those trailing slashes, just as it does for current_uri.

Changed 6 months ago by Geert

  • status changed from assigned to closed
  • resolution set to fixed

Fixed in r2310

Note: See TracTickets for help on using tickets.