2015-12-01 65 views
1

我正在尝试向Silex路由添加帐户。我的目标是有这样的路线:将帐户令牌添加到路由

/{_account}/{_locale}/ 
/{_account}/{_locale}/products 
/{_account}/{_locale}/block 

你可以找到我的代码here on github。这是一个小样本。我可以从请求中读取帐户令牌并将帐户保存在AccountListener中。

我尝试处理_account,如_locale。一旦设置或更新,应用程序就不必担心。这意味着如果我拨打$app['url_generator']->generate('blog'),将自动设置_account参数。

这是我目前的问题。我不知道如何通知UrlGenerator设置这些参数。

也许我的方法是完全错误的。

我希望你可以给我一些例子或食谱或somethink。或合并请求。

回答

0

UrlGenerator使用参数request_context(您可以看到in the code),因此您可以在侦听器中设置这些参数。

的src/app.php

$dispatcher = $app['dispatcher']->addSubscriber(
    new AccountListener(
     new AccountRepository(), 
     $app['request_context'], 
     $app['monolog'] 
    ) 
); 

SilexLab \监听\ AccountListener

public function __construct(
    AccountProvider $accountProvider, 
    RequestContext $requestContext, 
    Logger $logger 
) { 
    //... 
    $this->requestContext = $requestContext; 
} 

public function onKernelRequest(GetResponseEvent $event) 
{ 
    //... 

    $request->attributes->set('_account', $account); 

    $this->requestContext->setParameter('_account', $account); 
} 
+0

真棒,非常感谢。我会试一试! –