首先,我必须使用类" ParametersCompilerPass"来配置参数。从数据库中获取数据。以下是我的课程:
class ParametersCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$em = $container->get('doctrine.orm.default_entity_manager');
$boutique = $em->getRepository('AcmeBundle:Boutique')->findOneByNom($container->getParameter('boutique.config'));
if(null !== $boutique){
$container->setParameter('url_site', $boutique->getUrl());
$container->setParameter('idboutique', $boutique->getId());
}else{
$container->setParameter('url_site', null);
$container->setParameter('idboutique', 0);
}
}
}
当我从请求中设置参数时,它不起作用,我尝试添加此代码:
$request = $container->get('request_stack')->getCurrentRequest();
if($request->getMethod() == 'POST'){
if (null !== $choixbout = $request->get('choixbout')){
// $this->container->setParameter('idboutique',$choixbout);
}
}
服务request_stack返回null。
我不知道如何从POST变量配置参数。
希望你能帮助我。 感谢答案 0 :(得分:0)
是否确实要求设置参数?
创建一个具有请求依赖关系的服务可能很方便,该服务可以充当boutique
参数持有者。
例如
# app/config/services.yml
app.boutique:
class: AppBundle\Boutique\Boutique
arguments: ['@request_stack']
app.boutique_info_dependant1:
class: AppBundle\Boutique\BoutiqueDependant1
arguments: ['@app.boutique']
app.boutique_info_dependant2:
class: AppBundle\Boutique\BoutiqueDependant2
arguments: ['@app.boutique']
这将是一个参数处理程序。
# AppBundle/Boutique/Boutique.php
class Boutique
{
/** @var RequestStack */
private $requestStack;
/**
* BoutiqueListener constructor.
* @param ContainerInterface $container
*/
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function getBoutique()
{
$request = $this->requestStack->getCurrentRequest();
/// here you can add an extra check if the request is master etc.
if ($request->getMethod() == Request::METHOD_POST) {
if (null !== $choixbout = $request->get('choixbout')) {
return $choixbout;
}
}
return null;
}
}
然后使用处理程序
class BoutiqueDependant1
{
public function __construct(Boutique $boutique)
{
$this->myBoutique = $boutique->getBoutique();
}
}
这看起来不是最好的解决方案,但可以工作...... 其他选择是重新考虑应用程序架构,以不同的方式处理精品信息。