我刚刚发现了SensioLabsInsight,并发现了有关如何编写优秀代码的非常有趣的提示.如果对于为什么(或者为什么不)应该使用某些东西有一些解释,那就太好了 - 即使对于像exit和等的基本东西也是如此die.这将有助于我向与我合作的人解释事情.
所以我的问题是针对AccessDeniedHttpException - 它说:
Symfony应用程序不应抛出AccessDeniedHttpException
那么如何从应用程序控制器或EventListener返回403 Forbidden?
什么是最佳做法?
说实话,我以为会是
throw new AccessDeniedHttpException()
Run Code Online (Sandbox Code Playgroud)
因为404你有
throw $this->createNotFoundException()
Run Code Online (Sandbox Code Playgroud)
但看起来我错了.
我对Symfony(v 2.3.2)表格有一个奇怪的问题.没有关系,这是非常简单的形式.我还应该注意,此表单仅用于REST API.
所以我有一个已发布的字段(布尔值).在实体上,默认设置为false.
在更新时,REST API客户端发送正确的PUT请求...&[entity]published=0&....此值也在表单参数中的Symfony分析器中显示.
但是我注意到数据库中的实际值设置为true(或者因为它是tinyint).
所以,为了找出问题所在,我在之后添加了throw语句 $form->submit($request);
throw new \Exception(sprintf('Request: %s, form: %s', $request->get('entity')['published'], $form->get('published')->getData()));
要么
throw new \Exception(sprintf('Request: %s, form: %s', $request->get('entity')['published'], $form->getData()->getPublished()));
异常消息说:Request: 0, form: 1.这意味着在submit方法的某处,字符串值'0'被转换为1.
该领域是用 $builder->add('published', 'checkbox', [ 'required' => false ])
我也注意到奇怪的事情,这可能是相关的.在Symfony profiler,面板请求中,我收到错误:Warning: json_encode(): Invalid UTF-8 sequence in classes.php line 3758我没有发送任何奇怪的字符 - 只是单词"test".
假设你可以(new Object)->method()用PHP5.4 +我想知道为什么我不能这样做:
<?php
class Item {
public $property = 'test';
}
class Container
{
public function getItem()
{
return new Item();
}
}
echo get_class(($object = (new Container())->getItem())); // returns Item
// this comes up with error
echo ($object = (new Container())->getItem())->property;
Run Code Online (Sandbox Code Playgroud)
为什么最后一行代码会触发 PHP Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)
编辑:
看起来我需要澄清我的问题,因为我看到答案与我的问题完全无关.我不是在问如何摆脱语法错误.我的问题是:为什么我不能访问表达式上的属性($object = (new Container())->getItem()),同时get_class()告诉我这是Item的一个实例?