PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php – 如何使用Symfony表达式语言在@Security注释中使用类常量?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Symfony 3,我已经创建了一个@L_262_1@的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)
{

但是我不喜欢在应用程序中使用魔术字符串的想法,我宁愿使用类常量来进行检查.

像这样的东西:

/**
 * @Rest\Get("organisation/{iD}")
 * @Security("is_granted(AppBundle\OrgRoles::rOLE_ADMIN,request $request)
{

但是当我尝试时,我收到以下错误消息:

Unexpected character \"\\\" around position 20 for expression `is_granted(AppBundle\\OrgRoles::rOLE_ADMIN,id)`.

未转义时,如下:

Unexpected character "\" around position 20 for expression `is_granted(AppBundle\OrgRoles::rOLE_ADMIN,id)`.

所以我很难过.

可以吗?

有关更好的方法的任何建议吗?

您可以使用Expression Language Component中提供的 constant()函数
@Security("is_granted(constant('\\Full\\Namespace\\To\\OrgRoles::rOLE_ADMIN'),id)")

大佬总结

以上是大佬教程为你收集整理的php – 如何使用Symfony表达式语言在@Security注释中使用类常量?全部内容,希望文章能够帮你解决php – 如何使用Symfony表达式语言在@Security注释中使用类常量?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。