【问题标题】:How to use wildcard in Symfony's url_for helper?如何在 Symfony 的 url_for 助手中使用通配符?
【发布时间】:2010-10-19 07:48:06
【问题描述】:

在 routing.yml 中给出:

products:
url:   /products/*
param: { module: products, action: index }

如何使用 url_for 助手填充通配符部分?我的意思是这样的:

<?= url_for('products/index?wildcard=somthingRandom') ?>

生成 URL:

/products/somethingRandom

【问题讨论】:

    标签: php symfony1


    【解决方案1】:

    更改您的 routing.yml 以在匹配的 URL 中包含一个参数:

    products:
    url:   /products/:wildcard
    param: { module: products, action: index }
    

    然后你可以调用url_for 例如:

    <?php echo url_for("@products?wildcard=somethingRandom"); ?>
    

    (使用@products 表示在您的routing.yml 中使用名为products 的路由。)

    【讨论】:

    • 就是这样。我更喜欢使用数组语法: url_for("@products", array('wildcard' => 'somethingRandom'))
    • @Marc 是的 - 绝对更好读,特别是如果你的 URL 中有很多参数 ;-)
    猜你喜欢
    • 2010-11-01
    • 2011-07-04
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多