Search code examples
urlcakephpurl-routingcakephp-2.x

Why url parameter is indexed at 'named' instead of 'pass' in cakephp?


My url is: http://localhost/intranet/customers/search/1/1A%3A79%3A32%3A97%3AF2/1 I am printing the params object as follows:

print_r($this->params); 

This print the following array:

[params] => Array
        (
            [plugin] => 
            [controller] => customers
            [action] => search
            [named] => Array
                (
                    [1A] => 79:32:97:F2
                )

            [pass] => Array
                (
                    [0] => 1
                    [1] => 1
                )

        )

but it should print:

[params] => Array
        (
            [plugin] => 
            [controller] => customers
            [action] => search
            [named] => Array
                (

                )

            [pass] => Array
                (
                    [0] => 1
                    [1] => 1A:79:32:97:F2
                    [2] => 1
                )
        ) 

But when I change the url like: http://localhost/intranet/customers/search/1/abc/1

[params] => Array
        (
            [plugin] => 
            [controller] => customers
            [action] => search
            [named] => Array
                (

                )

            [pass] => Array
                (
                    [0] => 1
                    [1] => abc
                    [2] => 1
                )
        ) 

Which problem with 1A:79:32:97:F2 as parameter in url?


Solution

  • You need to disable the greediness of named parameter parsing, by default all parameters that look like named parameters are being parsed as such (that is values that contain the : separator).

    In your routes configuration, use the second argument of Router::connectNamed() to pass further options, something along the lines of this, which would enable only the default CakePHP pagination paramters:

    Router::connectNamed(
        false, // no custom named parameters
        array(
            'default' => true, // default pagination parameters
            'greedy' => false  // no greediness
        )
    );
    

    See also