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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵