getParam()始终返回null

问题描述 投票:1回答:1

我正在尝试使用路由器从URL获取项目ID。让我们说这是我的网址:http://boardash.test/tasks/all/7,我想在我的控制器中获得7。

我用这个创建了一个路由器:

$router->add(
    '/tasks/:action/{project}',
    [
        'controller' => 'tasks',
        ':action'    => 1
    ]
);

并尝试使用以下方法访问它:

$this->dispatcher->getParam('project');

但当我var_dump()这,它返回null

我错过了什么?

php phalcon phalcon-routing
1个回答
0
投票

:action占位符不正确。试试这样:

$router->add(
    '/tasks/:action/{project}',
    [
        'controller' => 'tasks',
        'action'    => 1 // <-- Look here
    ]
);

更新:经过几次测试后,当命名参数位于路径末尾时,似乎是混合数组/短语法中的错误。

这按预期工作并返回正确的参数。

// Test url: /misc/4444444/view
$router->add('/misc/{project}/:action', ['controller' => 'misc', 'action' => 2])

但是,这并没有为{project}返回正确的值。它返回“view”而不是“4444444”。

// Test url: /misc/view/4444444
$router->add('/misc/:action/{project}', ['controller' => 'misc', 'action' => 1])

文档中解释的语法:https://docs.phalconphp.com/en/3.2/routing#defining-mixed-parameters

稍后我会进行调查,但你可以考虑同时在github上提交一个问题。


临时解决方案:同时,如果紧急,您可以使用此解决方法。

$router->add('/:controller/:action/:params', ['controller' => 1, 'action' => 2, 'params' => 3])

// Test url: misc/view/test-1/test-2/test-3
$this->dispatcher->getParams() // array of all
$this->dispatcher->getParam(0) // test-1
$this->dispatcher->getParam(1) // test-2
$this->dispatcher->getParam(3) // test-3
© www.soinside.com 2019 - 2024. All rights reserved.