2016-11-28 6 views
4

が含まれていない場合は使用を除外する:http://example.com/entity/another-example.comのようなURLがアクションにマッチしていると、対応するanother-example.com実体が水和され、渡されたsymfonyの@ParamConverter:プレースホルダは、私はこのコントローラを持っているドット

/** 
* {@inheritdoc} 
* 
* @Route("entity/{domain_host}") 
* @ParamConverter("entity", class="AppBundle:Entity", options={ 
*  "repository_method" = "findOneByDomainHost", 
*  "mapping": {"domain_host": "domainHost"}, 
*  "map_method_signature" = true 
* }) 
*/ 
class EntityController extends Controller 
{ 
... 
} 

をこの方法でコントローラに送信します。

今、このエンティティにはIDもあります。

http://example.com/entity/12345のようなURLを傍受し、http://example.com/entity/another-example.comにリダイレクトしたいとします。

/** 
* {@inheritdoc} 
* 
* @Route("entity/{domain_host}") 
* @ParamConverter("store", class="AppBundle:Entity", options={ 
*  "repository_method" = "findOneByDomainHost", 
*  "mapping": {"domain_host": "domainHost"}, 
*  "map_method_signature" = true 
* }) 
*/ 
class EntityController extends Controller 
{ 
    public function redirectIdToDomainAction(Request $request) 
    { 
     die(dump($request)); 
     // Following will be the redirect logic 
    } 
} 

そして、私のrouting.ymlでは、ファイルの先頭に:

プレースホルダ場合実際上 redirectIdToDomainアクションが呼び出された
entity_id_to_domain: 
    path: /entity/{id} 
    defaults: { _controller: AppBundle:Entity:redirectIdToDomain } 
    requirements: 
     id: ^[^.]*$ 
    methods: [GET] 

私はEntityControllerで別のメソッドを作成し、これを行うには

ドットは含まれていません(ドットはdiscrimenです。プレースホルダにドットがある場合はドメインが渡され、ドットがない場合はプレースホルダはおそらくuts IDでエンティティを表し、リダイレクトする必要があります)。

コントローラークラスEntityController@ParamConverterを使用しているため、プレースホルダーがドメインとして解釈され、結果としてAppBundle:Entity object not found.が得られるという問題があります。

したがって、プレースホルダにドットがある場合にのみ、@ParamConverterを適用する方法はありますか?または、Not found例外をスローせずにredirectToIdActionを動作させるために使用できる他の方法はありますか?

+0

より良いが、リダイレクトのものおよび/または1つのアクションにparamconverterを入れていないのはなぜ – Matteo

+1

paramconverterカスタムのリスナーを実装しますそれはクラスの代わりにそれを必要とするか、またはあなたのリダイレクトアクションをその外に移動しますか? – Gerry

+1

'paramconverter'はもっと多くのアクションで必要です(約7)。私はクラスからリダイレクトアクションを移動する可能性について考えましたが、別のオプションがあることを願っていました。ここで尋ねられました。カスタムのパラメータコンバータを作成する代わりに、別のコントローラを作成し、リダイレクトアクション... – Aerendir

答えて

1

カスタムParamConverterがこのトリックを行うかもしれませんが、作成したくない場合は、IDとドメイン名に異なるルートを使用する必要があります。あなたのルーティングは、クラス全体ではなく行動そのものに行われるため

ソリューション1

は、私はあなたが別のコントローラクラスにリダイレクトアクションを移動する必要があります怖いです。

その後、あなたが選択的に2

代わり要件

/** 
*@Route(
    "entity/{domain_host}", 
    name="entity_domain", 
    requirements={"domain_host": "^(?=.*[\w])(?=.*[.]).+$"} 
*) 
*@ParamConverter(...) 
*/ 
someActionOnDomainAction() 
{ 
//... 
} 


/** 
*@Route(
    "entity/{id}", 
    name="entity_domain", 
    requirements={"id": "^[\d]$"} 
*) 
*/ 
redirectIdToDomainAction() 
{ 
//... 
} 

ソリューションを使用してIDとドメイン名を一致させることができ、あなたはfindOneByDomainHostOrID()のような何かにあなたのリポジトリ方法findOneByDomainHost()を変更し、それは両方のドメインと一致させることができ名前と数値ID。

このようにしてObject Not Foundのエラーが発生し、エンティティのドメイン名を取得して、同じコントローラアクションでリダイレクトを行うことができます。ここで

は例です:

/** 
* Class EntityController 
* 
* @Route("/entity/{domain_host}", name="domain_host") 
* @ParamConverter("domain_host", class="AppBundle:Entity", options={ 
*  "repository_method" = "findOneByHostOrID" 
* }) 
*/ 
class EntityController extends Controller 
{ 
    /** 
    * @Route("/", name="show_domain_host") 
    * @param Entity $domain_host 
    */ 
    public function domain_hostAction(Entity $domain_host, Request $request) 
    { 
     $id = $domain_host->getId(); 
     $host = $domain_host->getHost(); 

     // Get the unconverted route parameter. 
     $parameter = $request->attributes->get('_route_params')['domain_host']; 

     if ($parameter == $domain_host->getId()){ 
      // If the route parameter matches the ID, 
      // redirect to the same route using the domain host. 
      $url = $this->generateUrl('show_mail', ['mail' => $lib]); 
      return $this->redirect($url, 301); 
     } 

     return new Response("<html><body>$id $host</body></html>"); 
    } 
} 

とリポジトリクラス:

class EntityRepository extends EntityRepository 
{ 
    public function findOneByHostOrID($domain_host) 
    { 
     if (preg_match("[\\d]",$domain_host)) { 
      return $this->findOneById($domain_host); 
     } else { 
      return $this->findOneByHost($domain_host); 
     } 
    } 
} 

関連する問題