1

我正在尝试使用路由器从 url 获取项目 ID。假设这是我的 URL:http://boardash.test/tasks/all/7,我想在我的控制器中获取 7。

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

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

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

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

但是当我var_dump()这样做时,它会返回null

我错过了什么?

4

1 回答 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}。它返回“视图”而不是“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
于 2018-01-04T19:14:49.750 回答