dsvcqvp139098 2014-12-10 17:54
浏览 185
已采纳

Phalcon URL生成器混合自定义路由中的参数

I'm trying to get the following route to work:

$router->add('/([a-z]{2})/:namespace/:controller/:action/([^\/]+)', array(
    'language' => 1,
    'namespace' => 2,
    'controller' => 3,
    'action' => 4,
    'location' => 5
))->setName('location');

The relevant (and for testing purposes only) line in the Volt template looks like this:

{{  url({'for': 'location', 'namespace': 'weather', 'controller': 'forecast', 'action': 'precipitation', 'location': 'Hamburg' }) }}

What I want is //weather/forecast/precipitation/Hamburg but instead all I get is //weather/forecast/precipitation/.

Next thing I tried was

$router->add('/([a-z]{2})/:namespace/:controller/:action/{location:[^\/]+}', array(
    'language' => 1,
    'namespace' => 2,
    'controller' => 3,
    'action' => 4,
))->setName('location');

which at least gives me the location in the URL, but at a totally wrong position: //Hamburg/forecast/precipitation/.

Now I've looked into the Library\Mvc\Router and the array that is passed to get() looks fine to me:

Array
(
    [for] => location
    [namespace] => weather
    [controller] => forecast
    [action] => precipitation
    [location] => Hamburg
    [language] => en
)

I will use my own Router to handle translated URLs, so I think we can ignore the language parameter for now. So far, the custom Router does nothing more than call the original one.

Any idea how to get the location parameter to work?

  • 写回答

1条回答 默认 最新

  • duanmeng3573 2014-12-11 13:15
    关注

    I've tested all possibilities given in the router documentation and even take a look in the router implementation for Phalcon 2.0 (are you using this version, right?!). The only thing that worked out was to not use named parameters at all, even the built-in placeholders needed to be removed from the route pattern:

    $this->add('/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_]+)/([^\/]+)', array(
        'namespace' => 1,
        'controller' => 2,
        'action' => 3,
        'location' => 4,
    ))->setName('location');
    

    In other words, this seems like a bug to me. Feel free to file a bug to investigate further.

    About the language parameter, I don't know if you already saw this question, but you can deal with this in a similar way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程