【问题标题】:About the phalcon routes config ,is it the design bug?关于 phalcon 路由配置,是设计错误吗?
【发布时间】:2014-10-24 15:43:26
【问题描述】:
$config = array(
    'best-url' => array(
        'pattern' => '/best',
        'paths' => array(
            'controller' => 'cate',
            'action' => 'index?cate_id=1',
        ),
    ),
    'better-url' => array(
        'pattern' => '/better',
        'paths' => array(
            'controller' => 'cate',
            'action' => 'index?cate_id=2',
        ),
    ),
    'cate-url' => array(
        'pattern' => '/cate/index/:int',
        'paths' => array(
            'controller' => 'cate',
            'action' => 'index',
            'cate_id' => '1'
        ),
    )
);

我有一个要求,url 映射到 cate_id 像 /best -> /cate/index?cate_id=1,/better -> /cate/index?cate_id = 2,我不知道如何配置我的路线,我已经在上面解决了,但是当我使用时我需要它自动反向路由 $url->get('for'=>'cate-url','cate_id'=>1) 它不能创建像 /best 或 /better 这样的 url。 怎么解决,谢谢。

【问题讨论】:

  • 您不能在实际中传递这样的参数。动作只是动作名称。在 'best-url' 中,只需将 'cate_id' => '1' 参数添加到路径等等。

标签: url routes router phalcon


【解决方案1】:

正如@Arius 所说,代码应该是这样的,请务必检查routing the documentation

'best-url' => array(
    'pattern' => '/best',
    'paths' => array(
        'controller' => 'cate',
        'action' => 'index,
        'params' => array('cate_id'=>1)
    ),
)

另外,请确保您设置了基本 uri:

$di->set('url', function(){
    $url = new Phalcon\Mvc\Url();
    $url->setBaseUri('/');
    return $url;
});

PS:我从未见过像您展示的那样配置路由的方式。通常设置如下。我假设您遍历 $config 并将它们像这样添加到代码中更下方的某个位置?

//Define a route
$router->add(
    "/best",
    array(
        'controller' => 'cate',
        'action' => 'index,
        'params' => array('cate_id'=>1)
    )
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多