这个问题始于我不理解为什么我不能将变量传递给symfony2全局帮助函数(服务),但是由于人们比我更亮,我意识到我的错误是试图在类中使用security_context没注射它......
这是最终的结果,代码可行.我发现没有更好的办法让这对社区有帮助.
这是您可以从symfony2中的全局函数或辅助函数中获取security_context中的用户和其他数据的方法.
我有以下类和功能:
<?php
namespace BizTV\CommonBundle\Helper;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class globalHelper {
private $container;
public function __construct(Container $container) {
$this->container = $container;
}
//This is a helper function that checks the permission on a single container
public function hasAccess($container)
{
$user = $this->container->get('security.context')->getToken()->getUser();
//do my stuff
}
}
Run Code Online (Sandbox Code Playgroud)
...定义为服务(在app/config/config.yml中),就像这样......
#Registering my global helper functions
services:
biztv.helper.globalHelper:
class: BizTV\CommonBundle\Helper\globalHelper
arguments: ['@service_container']
Run Code Online (Sandbox Code Playgroud)
现在,在我的控制器中,我这样调用这个函数......
public function createAction($id) {
//do some stuff, transform $id into $entity of my type...
//Check if that …Run Code Online (Sandbox Code Playgroud) 我发现包含这个路径的模板工作正常
{% include 'AcmeDemoBundle:TemplateArchive:view.html.twig' with {'data': c.data} %}
Run Code Online (Sandbox Code Playgroud)
虽然这似乎是不允许的:
{% include 'AcmeDemoBundle:TemplateArchive:6:view.html.twig' with {'data': c.data} %}
Run Code Online (Sandbox Code Playgroud)
换句话说,我试图访问我已经整理到我的bundle/resources/views /文件夹中的子文件夹结构的模板.
如果我不允许比我的第一行中的常规单级包含更深入地钻取,是否有另一种/更好的方法来构造这些模板文件?
(文件夹名称'6'表示我想要包含的数据库中的模板ID,它需要是动态的,并且很好地排序在文件夹中......).
我已经尝试命名我的模板文件夹't6',但没有区别,"没有前导字母的数字"不是这里的问题...
我试图通过一个简单的方法得到最高的数字NaN:
data = [4, 2, 6, 1, 3, 7, 5, 3];
alert(Math.max(data));
Run Code Online (Sandbox Code Playgroud)
我已经读过如果数组中的一个值无法转换为数字,它将返回typeof,但在我的情况下,我已经仔细检查NaN以确保它们都是数字,所以可能是我的问题?
我有一个具有此属性的实体"容器"
/**
* @ORM\OneToMany(targetEntity="BizTV\ContentManagementBundle\Entity\Content", mappedBy="container")
*/
private $content;
Run Code Online (Sandbox Code Playgroud)
该属性是一个数组集合......
public function __construct() {
$this->content = new \Doctrine\Common\Collections\ArrayCollection();
}
Run Code Online (Sandbox Code Playgroud)
......用这两种标准方法
/**
* Add content
*
* @param BizTV\ContentManagementBundle\Entity\Content $content
*/
public function addContent(\BizTV\ContentManagementBundle\Entity\Content $content)
{
$this->content[] = $content;
}
/**
* Get content
*
* @return Doctrine\Common\Collections\Collection
*/
public function getContent()
{
return $this->content;
}
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,是否有一种平滑的方法可以在getContent()调用中构建一个排序功能?我不是php wiz,当然也没有在symfony2中经验丰富,但我随便学习.
内容实体本身有一个像这样的排序INT,我想对它进行排序:
/**
* @var integer $sortOrder
*
* @ORM\Column(name="sort_order", type="integer")
*/
private $sortOrder;
Run Code Online (Sandbox Code Playgroud) 而不是像我这样在我的TWIG中渲染每张幻灯片(见第6行):
{# loop out the slides #}
{% for c in contents %}
{% set i=i+1 %} {# increase slide number #}
<div id="slide{{ i }}" class="slide" style="z-index:{{ i }};">
{# the slide itself, rendered by it's own template #}
{% include 'BizTVArchiveBundle:ContentTemplate:'~c.template~'/view.html.twig' with {'contents': c, 'ordernumber': i} %}
</div>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
...相反,我想在控制器中保留所有逻辑,只是向视图提供一系列准备好的幻灯片.我怎么能做这样的事情(见第9行):
//do stuff...
foreach ($container->getContent() as $c) {
$content[$i]['title'] = $c->getTitle();
$content[$i]['sort'] = $c->getSortOrder();
$content[$i]['data'] = $c->getData();
$content[$i]['template'] = $c->getTemplate()->getId();
$content[$i]['duration'] = $this->extract_seconds($c);
$content[$i]['html'] = $this->render('BizTVArchiveBundle:ContentTemplate:'.$content[$i]['template'].'/view.html.twig', array( …Run Code Online (Sandbox Code Playgroud) 好吧,我现在已经在这两个小时了,我看到其他人有这个错误,但我似乎无法与他的原因/决议相匹配.
致命错误:require()[function.require]:无法在第55行的/var/www/biztv_symfony/vendor/symfony/src/Symfony/Component/ClassLoader/DebugUniversalClassLoader.php中重新声明类companycontroller
终端提供了一个更好的错误消息,指向我报告的实际类的结束子句(尝试重新声明).
如果我删除或重命名文件companyController.php它会抛出一个Symfony2错误,说它正在查找该类,但没有找到它在预期的位置.
如果我把文件放回原位,apache抛出一个php错误,说不能重新声明类companyController.
我只有一次吗?!
这是整个班级...如果有人有耐心尝试帮助我...
<?php
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use BizTV\BackendBundle\Entity\company;
use BizTV\BackendBundle\Form\companyType;
/**
* company controller
*
*/
class companyController extends Controller
{
/**
* Lists all company entities.
*
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$entities = $em->getRepository('BizTVBackendBundle:company')->findAll();
return $this->render('BizTVBackendBundle:company:index.html.twig', array(
'entities' => $entities
));
}
/**
* Finds and displays a company entity.
*
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('BizTVBackendBundle:company')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find company entity.'); …Run Code Online (Sandbox Code Playgroud) 我的后端有一个简单的用户管理器,我希望能够在不设置新密码/每次重复旧密码的情况下编辑用户.
现在,如果我在编辑用户时将密码字段留空,symfony2会抱怨必须输入密码,当然我在注册新用户时需要此功能,但是当我编辑它们时,我希望表单能够如果没有填写密码框,请忽略它们.
这是如何完成的?
当我在div上使用浏览器contenteditable = true以便让用户更新其中的文本时,我遇到了这个问题(使用Chrome):
当使用delete backspace键删除换行符(跳回一行)时,浏览器会在该文本周围插入一个带内联样式集的标记.
这实在令人沮丧,因为它不仅会这样做,还会为该span标记添加内联样式,例如,如果我的字体颜色当前为黑色,则会为该span标记添加style ="color:black".
结果是我无法再用我的工具栏编辑该文本的颜色,因为它已通过内联样式很难设置为span标记.如果我使用删除键备份一行,同样的事情发生在字体大小上.
这里的任何人可以教我一两件关于满足的事情,或建议一种方法来消除跨度:s,如果它不可能阻止这种浏览器行为..
**要重现此问题** - 在浏览器中创建一个div,在其上设置内联样式,例如font-size:36px - 使用浏览器中可编辑的内容编辑div的文本,写一些带有手动换行符的行. - 现在将光标放在FRONT OF/FRFORE的一段文本中,然后点击退格键.现在应该在光标前面的文本周围生成一个span标记,并改变它的样式.
更新** 所以我尝试了几种不同的解决方案,但收效甚微.首先我尝试删除所有标签,但之后我也丢失了所有的换行符(也许更好的正则表达式可以解决这个问题,我不是regExp写作的专家).
下面的所有函数首先调用keyup事件,但后来我将它们连接到取消选择文本框.如何解雇以下功能与问题无关.
//option to remove all tags
var withTags = $(textObject).html();
var withoutTags = withTags.replace(/<(?:.|\n)*?>/gm, '');
$(textObject).html(withoutTags);
Run Code Online (Sandbox Code Playgroud)
我的第二次尝试更成功,删除文本框下面的对象的样式标记(div添加了文本框内的divs),这是我的第一个代码
//option to remove style of elements
$(textObject).children().each(function() {
$(this).removeAttr('style');
console.log('removing style from first level element: '+this);
});
Run Code Online (Sandbox Code Playgroud)
然后我意识到每次编辑文本框时,chrome都可能会添加一个新的嵌套级别的div/span标签,上面的代码将无法达到,所以我这样做了:
//option to remove style of elements
$(textObject).children().each(function() {
$(this).removeAttr('style');
console.log('removing style from first level element: '+this);
//And of course it would be all …Run Code Online (Sandbox Code Playgroud) 我可以用jQuery选择#id> #id吗?
我有这个结构
<div id="slide1">
<div id="slide_body">
some content
</div>
</div>
<div id="slide2">
<div id="slide_body">
some content
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
有没有办法使用jquery只选择#slide1里面的#slide_body?
或者是将id附加到每个body div的唯一解决方案
<div id="slide2">
<div id="slide_2_slide_body">
some content
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 有没有办法生成证书或类似的东西,以便我的prod服务器可以从我的github仓库拉出来,而不是我每次都进行身份验证?