2016-08-20 101 views
-6

我不希望用戶在我的URL中看到參數。如何在Symfony2中隱藏URL中的參數

實施例:

my_route: 
    path:  /***/{id} 
    defaults: { _controller: MyBundle:Default:myaction} 

的路由將這樣產生:

*** /產品/ 1

我想只看到

***/product

+1

所以,當服務器收到'/ product'的直接請求,它應該如何決定顯示哪個產品? – Paulpro

+0

有Url重寫器如何做到這一點? –

+1

您可以將'/ product/1'重寫爲'/ product/potato-pie',而不是'/ product'。每個網址仍需要一些方法來唯一標識資源。 – Paulpro

回答

0

正如com這個請求需要一些東西來標識你想要獲取的資源。但這與URL重寫無關。如果要顯示資源,而不是ID的蛞蝓,請執行下列操作:

my_route: 
    path:  /products/{slug} 
    defaults: { _controller: MyBundle:Default:myaction} 

然後,在MyBundle /控制器/ DefaultController.php:

public function myactionAction(Request $request, $slug) 
{ 
    $product = $this->getDoctrine()->getManager()->getRepository('MyBundle:Product')->findOneBy(["slug" => $slug]); 
} 

,當你想創建這個產品的鏈接,使用

<a href="{{ path('my_route', {'slug': product.slug}) }}">link to the product</a>