我正在使用Symfony 3,我已经创建了一个自定义的Voter类.
我想使用SensioFrameworkExtraBundle @Security标签访问它.
它有点工作.
如果我执行以下操作,它将完美运行:
/**
* @Rest\Get("organisation/{id}")
* @Security("is_granted('OrgAdmin', id)")
* @param int $id
* @param Request $request
*
* @return View
*/
public function getOrganisationAction($id, Request $request)
{
Run Code Online (Sandbox Code Playgroud)
但是我不喜欢在应用程序中使用魔术字符串的想法,我宁愿使用类常量来进行检查.
像这样的东西:
/**
* @Rest\Get("organisation/{id}")
* @Security("is_granted(AppBundle\OrgRoles::ROLE_ADMIN, id)")
* @param int $id
* @param Request $request
*
* @return View
*/
public function getOrganisationAction($id, Request $request)
{
Run Code Online (Sandbox Code Playgroud)
但是当我尝试时,我收到以下错误消息:
Unexpected character \"\\\" around position 20 for expression `is_granted(AppBundle\\OrgRoles::ROLE_ADMIN, id)`.
Run Code Online (Sandbox Code Playgroud)
未转义时,如下:
Unexpected character "\" around position 20 for expression …Run Code Online (Sandbox Code Playgroud) 我试图让以下工作.
count循环需要遍历所有值,并且可能没有与每个计数关联的用户,但是i需要在每个循环中使用计数值来传递给JavaScript.
python部分:
users = {}
users[1]={}
users[1][id]=...
users[1][email]=...
...
count=[1,2,3,4,5,6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
Django模板部分:
{% for i in count %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1
email:{% if users.i.email %}'{{users.i.email}}'{% else %}null{% endif%}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这不会返回任何电子邮件.当我代替数量1为i在{% if user.i.email %}电子邮件返回用户的电子邮件地址.我在JavaScript中使用数据,因此如果它不存在则需要隐式为null.我似乎无法让Django将i变量识别为变量而不是值i.
使用[]不起作用,因为它会抛出无效的语法错误
email:{% if users.[i].email %}'{{users.[i].email}}'{% else %}null{% endif%}
Run Code Online (Sandbox Code Playgroud)
我试过用" with"声明
{% for i in count %}{% with current_user=users.i %}... …Run Code Online (Sandbox Code Playgroud) dictionary ×1
django ×1
expression ×1
for-loop ×1
namespaces ×1
php ×1
symfony ×1
templates ×1
variables ×1