【问题标题】:Phalcon PhP - how to use named routes inside a controllerPhalcon PhP - 如何在控制器中使用命名路由
【发布时间】:2016-07-18 19:48:35
【问题描述】:

我无法找到如何从 Phalcon PhP 控制器内的命名路由中获取 URL。这是我的路线:

$router->add(
    '/admin/application/{formUrl:[A-Za-z0-9\-]+}/{id:[A-Za-z0-9\-]+}/detail',
    [
        'controller' => 'AdminApplication',
        'action' => 'detail' 
    ]
)->setName("application-details");

我只想获取 URL,例如: domain.com/admin/application/form-test/10/detail 。使用下面的代码,我可以获得创建链接的 html,与 link_to 的结果相同。

$url = $this->tag->linkTo(
    array(
        array(
            'for' => 'application-details',
            'formUrl' => $form->url,
            'id' => $id
        ), 
        'Show'
    )
);

我想要的结果只是 URL。我在控制器动作中。我知道它一定很简单,我只是找不到例子。你能帮帮我吗?

感谢您的帮助!

【问题讨论】:

    标签: php phalcon phalcon-routing


    【解决方案1】:

    您应该使用 URL 帮助程序。示例:

    $url = $this->url->get(
        [
            'for' => 'application-details', 
            'formUrl' => $form->url,
            'id' => $id,
        ], 
        [
            'q' => 'test1',
            'qq' => 'test2',
        ]
    );
    

    如果需要,您可以为查询字符串参数传递第二个数组。

    根据你的路由定义,上面的输出应该是这样的:

    /admin/application/form-url/25/detail?q=test1&qq=test2

    Generating URIs in the docs的更多信息。

    【讨论】:

    • 谢谢!正是我需要的。
    猜你喜欢
    • 2015-01-13
    • 2016-04-05
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    相关资源
    最近更新 更多