简体   繁体   English

如何在树枝i Symfony中访问自定义对象?

[英]How to access custom Object in twig i Symfony?

Inside a controller I check if user has access to a video if not AccessDeniedHttpException is thrown. 在控制器内部,如果没有抛出AccessDeniedHttpException,我会检查用户是否有权访问视频。

//check if user has access to this asset or not
if(!$this->get("asset.access_manager")->hasAccess($asset)){
   throw new AccessDeniedHttpException($this->get("translator")->trans("exception.asset.access.unauthorized"));
}

how can I pass $asset object which contains all information about the video to the Twig which is used by exception to show message? 如何将包含有关视频的所有信息的$ asset对象传递给Twig(异常情况下用来显示消息)? By accessing $asset object I can print information about video on Exception page. 通过访问$ asset对象,我可以在“异常”页面上打印有关视频的信息。

Thank you 谢谢

if you want to check the permission (role?) I think the better way is: 如果您想检查权限(角色?),我认为更好的方法是:

{% if app.user.username is defined and is_granted('ROLE_ADMIN') %}
     //some awesome code here
{% else %}
     //without permission here
{% endif %}

First check if the user exist and later check his role. 首先检查用户是否存在,然后检查其角色。

Some documentation here 一些文件在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM