所以我试图围绕如何通过BundleNameExtention
类设置参数,该类位于包的DependencyInjection
文件夹中,而不是直接在config.yml
中定义{ p>
我使用的是symfony install附带的默认AppBundle
。
看起来非常直接在线阅读文档,在load
方法中我应该能够设置我想要的参数,这就是我做的事情
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\xmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$config['comments'] = "some value";
$container->setParameter('app.comments', $config['comments']);
}
public function getAlias(){
return 'app';
}
}
如何在我的树枝模板中访问此参数?
答案 0 :(得分:1)
实际上,您必须在扩展类中实现prepend
接口,并添加以下class AppExtension extends Extension implements PrependExtensionInterface{
// ... your other code
public function prepend(ContainerBuilder $container){
$configs = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration(new Configuration(), $configs);
$container->prependExtensionConfig('twig', array(
'globals' => array('app_comments', $config['comments'])));
}
}
函数:
app_comments
现在,您可以访问所有模板中的{{1}}。
答案 1 :(得分:0)
好的,这就是我最终要做的事情。
在一个包的library(xts)
library(highfrequency)
x<-read.table(text='datePickup dateAccepted
"2015-06-30 14:30:28" "2015-06-30 14:32:14"
"2015-07-03 21:25:14" "2015-07-03 21:28:50"
"2015-07-03 12:27:30" "2015-07-03 12:29:53"',header=T)
x<-apply(x,2,as.POSIXlt,format="%Y-%m-%d %H:%M:%S",tz="GMT")
tsx<-xts(as.vector(difftime(x$dateAccepted,x$datePickup,units = "secs")),order.by = as.Date(x$datePickup))
atsx<-aggregatets(tsx,on = "days",FUN = "mean",k = 1,dropna = T)
df<-data.frame(index(atsx),as.vector(atsx))
colnames(df)<-c("date","averageTimeDifferenceInSeconds")
df
date averageTimeDifferenceInSeconds
1 2015-06-30 106.0
2 2015-07-03 179.5
文件夹中以及DependencyInjection
类我必须使用AppExtension
类来实现Configuration
Docs
我的ConfigurationInterface
班级
AppExtension
这是我的namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AppExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\xmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('app.comments', $config['comments']);
}
public function getAlias()
{
return 'app';
}
}
班级
Configuration
最后在我的namespace AppBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('app');
$rootNode->children()->scalarNode('comments')->end();
return $treeBuilder;
}
}
app/config/config.yml
现在要访问它,只需通过控制器获取它并将其传递给树枝模板。
app:
comments: 'some value'
这对我有用,希望它也可以帮助别人