We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Routing only works if parameter is numeric or has tilt sign.

I have the following route setup.

    $router->add('/schools', array(
        'module' => 'schools',
        'namespace'=>'MyNameSpace\Schools\Controllers\\',
        'controller'=>'index',
        'action' => 'index'
    ));

    $router->add('/schools/:params',array(
        'module' => 'schools',
        'namespace'=>'MyNameSpace\Schools\Controllers\\',
        'controller'=>'index',
        'action' => 'index',
        'params' => 1
    ));

    $router->add('/schools/:action',array(
    'module' => 'schools',
    'namespace'=>'MyNameSpace\schools\Controllers\\',
    'controller'=>'index',
    'action'=>1
    ));

    $router->add('/schools/:action/:params',array(
        'module' => 'schools',
        'namespace'=>'MyNameSpace\schools\Controllers\\',
        'controller'=>'index',
        'action'=>1,
        'params'=>2
    ));

Problem:

1. https://www.example.com/schools/23 Works fine

2. https://www.example.com/schools/~school-name Works as well

But,

3. https://www.example.com/schools/school-name does not work

Where school-name, ~school-name and 23 in the above URLs are parameters to the default action (index) of the controller.

I cannot print anything in the initialize method of the controller. Tried putting try catch on main method of index.php as well, no errors.

I can't print anything when 3rd URL above is executed, I just get 1 printed on the browser, no other errors. I then, printed matched route path in https://www.example.com/schools/~school-name and it gave

Phalcon\Mvc\Router\Route Object
(
    [_pattern:protected] => /schools/:params
    [_compiledPattern:protected] => #^/schools(/.*)*$#
    [_paths:protected] => Array
        (
            [module] => schools
            [namespace] => MyNameSpace\Schools\Controllers\
            [controller] => index
            [action] => index
            [params] => 1
        )

    [_methods:protected] => 
    [_hostname:protected] => 
    [_converters:protected] => 
    [_id:protected] => 34
    [_name:protected] => 
    [_beforeMatch:protected] => 
    [_group:protected] => 
)

Following route, the object is printed on https://www.example.com/schools/23

Phalcon\Mvc\Router\Route Object
(
    [_pattern:protected] => /schools/:action/:params
    [_compiledPattern:protected] => #^/schools/([a-zA-Z0-9\_\-]+)(/.*)*$#
    [_paths:protected] => Array
        (
            [module] => schools
            [namespace] => MyNameSpace\Schools\Controllers\
            [controller] => index
            [action] => 1
            [params] => 2
        )

    [_methods:protected] => 
    [_hostname:protected] => 
    [_converters:protected] => 
    [_id:protected] => 36
    [_name:protected] => 
    [_beforeMatch:protected] => 
    [_group:protected] => 
)

Can anybody help me, what I m doing wrong here? Thanks

Which Phalcon version?



2.9k
edited Feb '19

Do you have in the beginning of the route setup module $router = new \Phalcon\Mvc\Router(false); also: https://docs.phalcon.io/3.4/en/dispatcher

edited Feb '19

Also checked this.. and if getActiveMethod does not have camel-case filter then it will fail to resolve method and will trigger error.

Solution:

  1. add camelize to router
  2. change order, because /action/params matches your params, so You need to make them "parameter on index" first and only when specific action + (!!!) some parameter then use this action
  3. You will have problem if You need to call mySuperSchoolAction() on /my-super-school, but this will still be able to call /my-super-school/dummy

For me it works very weel, ORDER matters :)

    $router->add('/:module/:controller/:action/:params', [
        'module' => 1,
        'controller' => 2,
        'action' => 3,
        'params' => 4
    ])->convert('action', function($action) {
        return \Phalcon\Text::camelize($action);
    }); 

    $router->add('/:module/:controller/:params', [
        'module' => 1,
        'controller' => 2,
        'action' => 'index',
        'params' => 3
    ])->convert('action', function($action) {
        return \Phalcon\Text::camelize($action);
    });