我的路线(slug包含破折号!):
region:
pattern: /regione/{slug}-{id}
defaults: { _controller: SWAItaliaInCifreBundle:Default:region }
Run Code Online (Sandbox Code Playgroud)
在Twig模板中:
{% for r in regions %}
<a href='{{ path('region', { 'slug':r.slug, 'id':r.id }) }}'>{{ r.name }}</a>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
我收到关于正则表达式匹配的错误.问题:为什么Symfony2不允许在URL中使用破折号?如何指定我的路线包含短划线(并且它完全没问题)?
在渲染模板期间抛出异常("参数"slug"for route"区域"必须匹配"[^/ - ] +?"(给出"valle-d-aosta-vallee-d-aoste"). ")
my_bundle:
algorithm: blowfish # One of 'md5', 'blowfish', 'sha256', 'sha512'
Run Code Online (Sandbox Code Playgroud)
此配置由此配置树完成:
// Algorithms and constants to check
$algorithms = array(
'md5' => 'CRYPT_MD5',
'blowfish' => 'CRYPT_BLOWFISH',
'sha256' => 'CRYPT_SHA256',
'sha512' => 'CRYPT_SHA512',
);
$rootNode
->children()
->scalarNode('algorithm')
->isRequired()
->beforeNormalization()
->ifString()
->then(function($v) { return strtolower($v); })
->end()
->validate()
->ifNotInArray(array_keys($algorithms))
->thenInvalid('invalid algorithm.')
->end()
->validate()
->ifTrue(function($v) use($algorithms) {
return 1 != @constant($algorithms[$v]);
})
->thenInvalid('algorithm %s is not supported by this system.')
->end()
->end()
->end();
Run Code Online (Sandbox Code Playgroud)
由于每个算法都需要不同的参数,如何根据所选算法动态添加它们作为根节点的子节点?
例如,如果算法是"blowfish",那么应该有一个名为"cost"的标量节点,而如果"sha512"是一个标量节点"rounds",每个节点都有不同的验证规则.
编辑:我真正需要的是弄清楚当前的算法(如何处理$rootNode?)而不是调用:
$rootNode->append($this->getBlowfishParamsNode());
$rootNode->append($this->getSha256ParamsNode());
$rootNode->append($this->getSha512ParamsNode());
Run Code Online (Sandbox Code Playgroud)
编辑 …
我的配置节点可以source同时支持string和array值吗?
采购自string:
# Valid configuration 1
my_bundle:
source: %kernel.root_dir%/../Resources/config/source.json
Run Code Online (Sandbox Code Playgroud)
采购自array:
# Valid configuration 2
my_bundle:
source:
operations: []
commands: []
Run Code Online (Sandbox Code Playgroud)
扩展类可以区分它们:
if (is_array($config['source']) {
// Bootstrap from array
} else {
// Bootstrap from file
}
Run Code Online (Sandbox Code Playgroud)
我可能会使用这样的东西:
$rootNode->children()
->variableNode('source')
->validate()
->ifTrue(function ($v) { return !is_string($v) && !is_array($v); })
->thenInvalid('Configuration value must be either string or array.')
->end()
->end()
->end();
Run Code Online (Sandbox Code Playgroud)
但是我如何source在变量节点的结构(操作,命令等)上添加约束(只应在其值为类型时强制执行array)?
何我可以根据大写字符将$param字符串分解$chunks成碎片?
$string = 'setIfUnmodifiedSince';
$method = substr($string, 0, 3);
$param = substr($string, 3);
// Split $param and implode with '-' separator
$chunks = splitAtUpperCase($param); // Chunks are: 'If', 'Unmodified' and 'Since'
$field = implode('-', $chunks); // Get If-Unmodified-Since HTTP field name
Run Code Online (Sandbox Code Playgroud) 是否可以删除不需要的包以保持项目清洁?我正在使用Symfony2和propel来构建一个RESTful接口.不需要:
我找不到任何操作方法来删除不需要的包.任何帮助深表感谢.
编辑:monlog是记录器,而不是mongodb.需要它!
关于deps.lock文件:删除包后可以删除它,而不是问题:
php bin/vendors update
Run Code Online (Sandbox Code Playgroud)
我应该重新创建.它维护每个捆绑包检出的git版本ID.
当argument是PHP闭包时,可以指定默认参数值吗?喜欢:
public function getCollection($filter = function($e) { return $e; })
{
// Stuff
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么(可能是不同的语法?)或根本不可能?当然我知道我能做到:
public function getCollection($filter = null)
{
$filter = is_callable($filter) ? $filter : function($e) { return $e; };
// Stuff
}
Run Code Online (Sandbox Code Playgroud)
(注意:我没有测试上面的代码)
我有一个像这样的混合阵列(手机号码和实体):
$targets = array();
$targets[] = '+32647651212';
$targets[] = new Customer();
Run Code Online (Sandbox Code Playgroud)
在我的Twig模板中,我必须调用getMobile()if target是a Customer或只是打印数字,如果它实际上是一个数字(string).
instanceof在Twig中有类似操作符的东西吗?
<ul>
{% for target in targets %}
<li>{{ target instance of MyEntity ? target.getMobile : target }}</li>
{% else %}
<li>Nothing found.</li>
</ul>
Run Code Online (Sandbox Code Playgroud) 我必须在添加子句后立即向OR返回的查询构建器动态添加表达式.我找不到任何合适的方法,我刚刚开始学习Doctrine.getListQueryBuilderwhere
如何"链接"给定数量orX并将其添加到我的构建器中?
public function getListQueryBuilder($ownerId)
{
$qb = $this->createQueryBuilder('t');
return $qb
->where($qb->expr()->eq('t.user', ':user'))
->setParameter('user', $ownerId);
}
$builder = getListQueryBuilder(4);
// $ORs is a dynamically builded array, here is just an example
$ORs = array();
$ORs[] = $builder->expr()->like("t.name", 'my name');
$ORs[] = $builder->expr()->like("t.description", 'desc');
// Adding ORs to the builder
$builder->andWhere($builder->expr()->orX(/* here */));
Run Code Online (Sandbox Code Playgroud) include with only在Twig中真正的目的是什么:
{# only the foo variable will be accessible #}
{% include 'child.html.twig' with {'foo': 'bar'} only %}
Run Code Online (Sandbox Code Playgroud)
也许一些性能优势?或者只是为了避免在包含的模板中覆盖变量?作为文件:
包含的模板可以访问活动上下文的变量.您可以通过附加唯一关键字来禁用对上下文的访问.
我如何单元测试ContainsItalianVatinValidator自定义验证器,但w*不访问容器*和validator服务(从而创建存根对象)?
class ContainsItalianVatinValidator extends ConstraintValidator
{
/**
* @param mixed $value
* @param \Symfony\Component\Validator\Constraint $constraint
*/
public function validate($value, Constraint $constraint)
{
if (!preg_match('/^[0-9]{11}$/', $value, $matches)) {
$this->context->addViolation($constraint->message, array(
'%string%' => $value
));
}
// Compute and check control code
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试用例中,我知道我应该访问ConstraintViolationList,但我不知道如何从验证器本身做到这一点:
class ContainsItalianVatinValidatorTest extends \PHPUnit_Framework_TestCase
{
public function testEmptyItalianVatin()
{
$emptyVatin = '';
$validator = new ContainsItalianVatinValidator();
$constraint = new ContainsItalianVatinConstraint();
// Do the validation
$validator->validate($emptyVatin, $constraint);
// …Run Code Online (Sandbox Code Playgroud)