1

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
));

Problem:

1.  http://www.example.com/schools/23 

Works fine

2.  http://www.example.com/schools/~23

Works as well

But,

3.  http://www.example.com/schools/school-name

does not work, Where school-name, ~23 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 http://www.example.com/schools/~23 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 http://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] => 
)

UPDATE Surprisingly following url also works

http://www.example.com/schools/~school-name but not http://www.example.com/schools/school-name

Phalcon\Mvc\Router\Route Object
(
    [_pattern:protected] => /schools/:params
    [_compiledPattern:protected] => #^/schools(/.*)*$#
    [_paths:protected] => Array
        (
            [module] => agencies
            [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] => 
)

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

WatsMyName
  • 4,240
  • 5
  • 42
  • 73

1 Answers1

1

If You see this [_pattern:protected] => /schools/:action/:params and your code does not have this rule, then it looks like default route applied.

Create Router with false to disable default routes.

in Phalcon code it is clearly set to use default one: https://docs.phalconphp.com/3.4/en/api/Phalcon_Mvc_Router

public function __construct(bool! defaultRoutes = true)

  • I do have `/schools/:action/:params` route defined in actual code. But how come 2 example rules are working just because the parameter is numeric or has tilt sign?? And not working if alphabets (third example in my post)?? – WatsMyName Feb 19 '19 at 03:21
  • `http://www.example.com/schools/~school-name` also works but not `http://www.example.com/schools/school-name` – WatsMyName Feb 19 '19 at 04:44
  • Rules work in reverse order. If :action defined as last and matched then it will be picked up. – Juris Zeltiņš Feb 19 '19 at 07:10