1

我有以下路线设置。

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

问题:

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

工作正常

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

也可以

但,

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

不起作用, Whereschool-name~23上面的 URL 是控制器23默认操作的参数。(index)

我无法initialize在控制器的方法中打印任何内容。尝试将 try catch 也放在main方法上 index.php,没有错误。

执行上面的第三个 URL 时,我无法打印任何内容,我只是1在浏览器上打印,没有其他错误。然后,我打印了匹配的路由路径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] => 
)

跟随路线,对象被打印在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] => 
)

更新 令人惊讶的以下网址也有效

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] => 
)

谁能帮助我,我在这里做错了什么?谢谢

4

1 回答 1

1

如果您看到这一点[_pattern:protected] => /schools/:action/:params 并且您的代码没有此规则,那么它看起来像应用了默认路由。

创建Routerfalse禁用默认路由。

在 Phalcon 代码中,它明确设置为使用默认值:https ://docs.phalconphp.com/3.4/en/api/Phalcon_Mvc_Router

public function __construct(bool! defaultRoutes = true)

于 2019-02-18T11:40:06.120 回答