cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG] Dispatcher get invalid params / Router issue

Open kutsy opened this issue 13 years ago • 4 comments

Hello. I found bug with using router with integer parameters (that should not be changed).

My $router contain rule:

$router->add
( 
    '/video_news/all/page/{page:[0-9]+}/category/{category:[0-9]+}/interval/{interval:[0-9]+}/sortby/{sortby:(hits|comments|date)+}',
    [
        'controller'    => 'page', 
        'action'        => 'build', 
        'lang_switcher' => 1,  // this parameter should not be changed !!!
        'media_type'    => 3,  // this parameter should not be changed !!!
        'module_id'     => 8,  // this parameter should not be changed !!!
    ]
);

And PageController.php is

class PageController extends Phalcon\Mvc\Controller
{
    public function buildAction()
    {       
        print_r($this->dispatcher->getParams());
        die();
    }
}

At URL /video_news/all/page/30/category/0/interval/5/sortby/date I get output:

Array
(
    [lang_switcher] => 30    // THIS IS INVALID !!!
    [media_type] => 5        // THIS IS INVALID !!!
    [module_id] => 8
    [page] => 30
    [category] => 0
    [interval] => 5
    [sortby] => date
)

You can see, that params lang_switcher and media_type is invalid!

Phalcon v1.0.0, FreeBSD

kutsy avatar Apr 02 '13 05:04 kutsy

It's not a bug

/video_news/all/page/{page:[0-9]+}/category/{category:[0-9]+}/interval/{interval:[0-9]+}/sortby/{sortby:(hits|comments|date)+}
                     ↑ 1 (lang_switcher)    ↑ 2                        ↑ 3 (media_type)         ↑ 4     ↑ 5

iforp avatar Apr 02 '13 09:04 iforp

It's not a bug

OK, maybe, but I need some params for route rule that should not change. How can I do this?

kutsy avatar Apr 02 '13 09:04 kutsy

I think this is the only solution if you need default numeric params (which overrides the path params)

$router->add
( 
    '/video_news/all/page/{page:[0-9]+}/category/{category:[0-9]+}/interval/{interval:[0-9]+}/sortby/{sortby:(hits|comments|date)+}',
    [
        'controller'    => 'page', 
        'action'        => 'build', 
        'lang_switcher' => 1,  // this parameter should not be changed !!!
        'media_type'    => 3,  // this parameter should not be changed !!!
        'module_id'     => 8,  // this parameter should not be changed !!!
    ]
)
    ->convert('lang_switcher', function($param){return 1;})
    ->convert('media_type', function($param){return 3;})
    ->convert('module_id', function($param){return 8;});

$router->handle('/video_news/all/page/30/category/0/interval/5/sortby/date');
print_r($router->getParams());
Array
(
    [lang_switcher] => 1
    [media_type] => 3
    [module_id] => 8
    [page] => 30
    [category] => 0
    [interval] => 5
    [sortby] => date
)

iforp avatar Apr 02 '13 09:04 iforp

I think this is the only solution if you need default numeric params

OK, I do that. Thank you very much.

kutsy avatar Apr 02 '13 09:04 kutsy