2012-08-10 82 views
2

在Symfony2中,可以定義2個不同的@routes以獲得一個相同的控制器動作如何檢查在Symfony2中哪個@route觸發Controller :: Action?

問題是:如何檢查用戶來自哪個路徑或路徑的獨特行爲?

例子:假設我們有一個名爲"createUserAction"一個動作既能從@routes /common_register/premium_register到達。

在動作內部,我想區分兩種用戶,使用不同的表單並根據他們輸入的路線創建用戶(或者通常根據路徑有不同的行爲)。

我該怎麼做?

回答

4

在你的行動,只需要添加額外的特殊路徑參數$_route

public function createUserAction ($_route) 
{ 
    ... //$_route will return the name of your route 
} 
+0

這麼好的一個!我愛symfony2 :) – ElPiter 2012-08-10 14:40:17

1

你有沒有考慮另一種方法的方法?只需使用具有參數的單一路線:

/** 
*@route ("/register/{type}", requirements={"type" = "common|premium"}) 
**/ 

public function createUserAction ($type) { 
    //use $type to decide what to do 
} 
相關問題