简体   繁体   中英

Route not working when parameter passed to default action

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM