我正在使用Symfony 2.7和Sonata Admin Bundle来管理一些产品和产品图像.我使用了Sonata Admin Cookbook食谱:https://sonata-project.org/bundles/admin/master/doc/cookbook/recipe_file_uploads.html用于图像.
由于图片必须具有与之关联的产品ID,因此我想禁用Sonata管理信息中心和顶部工具栏中的"添加新图片"链接,因此任何上传的图片都会包含相关产品.实际上,唯一允许添加图像的地方是产品添加/编辑页面.
根据这里找到的一些答案,我试图删除这样的路线:Sonata Admin Dashboard:为每个实体配置操作
protected function configureRoutes(RouteCollection $collection)
{
$container = $this->getConfigurationPool()->getContainer();
if ($container->get('request')->get('_route') == 'sonata_admin_dashboard') {
$collection->remove('create');
}
}
Run Code Online (Sandbox Code Playgroud)
但是这个解决方案并不好,因为如果在我访问管理仪表板时初始化缓存,则路径会在任何地方被删除,但如果缓存在不同的页面上初始化,则路由将出现在所有页面上,包括仪表板,因为如果在显示链接时路径存在,Sonata Admin会在模板中验证.
所以,我需要存在的路由并删除链接.这可以使用配置完成,还是我必须重写模板?
我想在不使用bundle的情况下使用Twitter Bootstrap和Symfony 2.我已经设法安装MopaBootstrapBundle,但决定删除它并使用普通TB.
composer.json
"require": {
"twbs/bootstrap": "dev-master"
}
Run Code Online (Sandbox Code Playgroud)
因此,在使用composer安装后,路径[project_path]/vendor/twbs/bootstrap与以下内容相同:https://github.com/twbs/bootstrap
config.yml
# Assetic Configuration
assetic:
debug: %kernel.debug%
use_controller: false
bundles: [ ]
filters:
cssrewrite: ~
less:
node: /usr/bin/nodejs
node_paths: [/usr/lib/nodejs:/usr/lib/node_modules]
apply_to: "\.less$"
Run Code Online (Sandbox Code Playgroud)
我为我的项目创建了一个新的包,AcmeDemoBundle并添加了[project_path]/src/Acme/DemoBundle/Resources/public/less包含两个文件的文件夹:
variables.less- [project_path]/vendor/twbs/bootstrap/less/variables.less我可以修改的副本,而不会影响原来的TB包style.less style.less内容:
@import "../../../../../../vendor/twbs/bootstrap/less/bootstrap.less";
@import "variables.less";
// any local changes should go below this line
[some local less code]
Run Code Online (Sandbox Code Playgroud)
在 base.html.twig
{% stylesheets '@AcmeDemoBundle/Resources/public/less/style.less' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets …Run Code Online (Sandbox Code Playgroud) 我们正在构建一个Symfony 2应用程序,它将一些数据从控制器发送到视图:
$user = array(
'configuration' => array(
'levels' => array(
'warning' => 0.05,
'danger' => 0.10,
),
),
);
return $this->render(
'MyWebsiteBundle:Core:searchResults.html.twig',
array(
'userJSON' => json_encode($user)
)
);
Run Code Online (Sandbox Code Playgroud)
<script language="javascript">
user = $.parseJSON("{{ userJSON }}");
</script>
Run Code Online (Sandbox Code Playgroud)
在dev结果看起来像这样和按预期工作:
user = $.parseJSON("\x7B\x22configuration\x22\x3A\x7B\x22levels\x22\x3A\x7B\x22warning\x22\x3A0.05,\x22danger\x22\x3A0.1\x7D\x7D\x7D");
Run Code Online (Sandbox Code Playgroud)
另一方面,prod结果以不同的方式编码,从而在控制台中显示错误:
user = $.parseJSON("{"configuration":{"levels":{"warning":0.05,"danger":0.1}}}");
Run Code Online (Sandbox Code Playgroud)
控制台错误:未捕获的SyntaxError:意外的令牌&
什么产生这种差异?
我正在使用Twitter Bootstrap构建一个项目.在文档中说明:
按钮标签
使用一个按钮类
<a>,<button>或<input>元素.[...]
跨浏览器渲染
作为最佳实践,我们强烈建议
<button>尽可能使用该元素以确保匹配跨浏览器呈现.
这是一个很好的练习SEO明智的说法吗?
在Symfony2项目中,我有一个Doctrine实体,它有一个datetime叫做的字段lastAccessed.此外,实体在字段上使用TimestampableupdatedAt.
namespace AppBundle\Entity;
use
Doctrine\ORM\Mapping as ORM,
Gedmo\Mapping\Annotation as Gedmo
;
class MyEntity {
/**
* @ORM\Column(type="datetime")
*/
private $lastAccessed;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
}
Run Code Online (Sandbox Code Playgroud)
我需要在不更新字段的lastAccessed情况下更新updatedAt字段.我怎样才能做到这一点?
我正在为 Symfony 4 开发一个包,它的结构如下:
\Acme
\FooBundle
\Article
\Entity
- Article.php
- Comment.php
\Form
- ArticleType.php
\Repository
- ArticleRepository.php
- CommentRepository.php
- ArticleManager.php
\User
\Entity
- User.php
\Repository
- UserRepository.php
- UserManager.php
\SomethingElse
\Entity
- SomethingElse.php
\Repository
- SomethingElseRepository.php
- SomethingElseManager.php
Run Code Online (Sandbox Code Playgroud)
还有更多的文件夹和实体,但与问题无关。
可以使用如下配置创建自动装配该文件夹中的所有类:
Acme\FooBundle\:
resource: '../../*/{*Manager.php,Repository/*Repository.php}'
exclude: '../../{Manager/BaseManager.php,Repository/BaseRepository.php}'
autowire: true
Run Code Online (Sandbox Code Playgroud)
但是当您需要添加诸如 的服务标签时doctrine.repository_service,这种配置将无济于事。没有标签,在控制器中使用时,如:
$this->getDoctrine()->getRepository(Bar::class)
Run Code Online (Sandbox Code Playgroud)
或者
$this->getDoctrine()->getManager()->getRepository(Bar::class)
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误:
“Acme\FooBundle\SomethingElse\Repository\SomethingElseRepository”实体仓库实现了“Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepositoryInterface”,但是找不到它的服务。确保服务存在并标记为“doctrine.repository_service”。
问题是,因为它们都驻留在同一个根文件夹中,所以我不允许使用如下配置,因为它会有重复的Acme\FooBundle\键:
Acme\FooBundle\:
resource: '../../*/{*Manager.php}'
exclude: '../../{Manager/BaseManager.php}'
autowire: true
Acme\FooBundle\:
resource: '../../*/{Repository/*Repository.php}'
exclude: '../../{Repository/BaseRepository.php}'
autowire: true
tags: ['doctrine.repository_service']
Run Code Online (Sandbox Code Playgroud)
所以,我想知道是否有我找不到的解决方法,或者我应该手动添加每一个服务?
编辑: 能够在类中使用注释本来是一个不错的功能,因此当它被加载时它“知道”它的标签,但我认为它反过来工作,加载一个类,因为它被标记为某个标签。
我知道...远离全球。
实际上,我确实需要拥有一个可访问的值,并且最重要的是可以从应用程序的不同部分进行修改。这是我出于调试目的需要监视的某些操作的计数器。搜寻与Symfony和全局变量有关的任何内容,总是让我得到建议使用Container参数或Twig全局变量的结果,但根据Symfony文档,事实是:
您只能在编译容器之前设置一个参数:不能在运行时设置。
考虑到我需要在控制器中而不是在视图中使用,Twig全局变量几乎超出了范围。
恕我直言,这两个解决方案都更像是常量而不是变量。
因此,问题是:是否有最佳实践来使用Symfony获得我需要的东西,还是应该只使用PHP全局变量?
谢谢!
我正在一个项目中使用SonataAdminBundleand 。安装的软件包有:SonataUserBundleSymfony 2
$ composer show | grep symfony
friendsofsymfony/rest-bundle 1.7.7 This Bundle provides various tools to rapidly develop RESTful API's with Symfony
friendsofsymfony/user-bundle v1.3.6 Symfony FOSUserBundle
symfony/assetic-bundle v2.8.0 Integrates Assetic into Symfony2
symfony/css-selector v2.8.6 Symfony CssSelector Component
symfony/dom-crawler v2.8.6 Symfony DomCrawler Component
symfony/monolog-bundle 2.11.1 Symfony MonologBundle
symfony/polyfill-apcu v1.1.1 Symfony polyfill backporting apcu_* functions to lower PHP versions
symfony/polyfill-mbstring v1.1.1 Symfony polyfill for the Mbstring extension
symfony/swiftmailer-bundle v2.3.11 Symfony SwiftmailerBundle
symfony/symfony v2.7.13 The Symfony PHP framework …Run Code Online (Sandbox Code Playgroud)