简体   繁体   English

参数传递给默认操作时路由不起作用

[英]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.不起作用,上述 URL 中的school-name~2323是控制器默认操作(index)的参数。

I cannot print anything in the initialize method of the controller.我无法在控制器的initialize方法中打印任何内容。 Tried putting try catch on main method of index.php as well, no errors.尝试将 try catch 也放在index.php main方法上,没有错误。

I can't print anything when 3rd URL above is executed, I just get 1 printed on the browser, no other errors.执行上面的第三个 URL 时,我无法打印任何内容,我只在浏览器上打印了1 ,没有其他错误。 I then, printed matched route path in http://www.example.com/schools/~23 and it gave然后,我在http://www.example.com/schools/~23打印了匹配的路由路径,它给出了

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按照路线,对象打印在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 http://www.example.com/schools/~school-name但不是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.如果你看到这个[_pattern:protected] => /schools/:action/:params并且你的代码没有这个规则,那么它看起来像是应用了默认路由。

Create Router with false to disable default routes.使用false创建Router以禁用默认路由。

in Phalcon code it is clearly set to use default one: https://docs.phalconphp.com/3.4/en/api/Phalcon_Mvc_Router在 Phalcon 代码中,它明确设置为使用默认值: https : //docs.phalconphp.com/3.4/en/api/Phalcon_Mvc_Router

public function __construct(bool! defaultRoutes = true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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