我正在尝试找到一个最可重用的工作选项,以便能够从FormType进行翻译.
我的第一个选择是以这种方式专门为每个FormType声明一个服务:
services.yml
form.enquiry:
class: Acme\DemoBundle\Form\EnquiryType
arguments: [@translator]
Run Code Online (Sandbox Code Playgroud)
EnquiryType.php
use Symfony\Component\Translation\Translator;
class EnquiryType extends AbstractType {
public $translator;
public function __construct(Translator $translator=null)
{
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$tr= $this->translator;
$msg=$tr->trans('default_error');
$builder->add ...
Run Code Online (Sandbox Code Playgroud)
MyController.php
$form = $this->container->get('form.enquiry')->create();
return $this->render('AcmeDemoBundle:Home:index.html.twig', array(
'form' => $form->createView()
));
Run Code Online (Sandbox Code Playgroud)
给出了这个错误
FatalErrorException:错误:调用未定义的方法Acme\DemoBundle\Form\EnquiryType :: create()
我想知道如何通过更改代码来解决它,甚至更好地找到一个更好的选项,允许我将转换器服务注入任何FormType,而无需单独声明每个FormType服务.
我遵循与 DASH 视频配合使用的 shaka 的基本用法,但在尝试加载 M3U8 时抛出错误代码 4032。
import * as muxjs from 'mux.js';
import * as shaka from 'shaka-player';
export class AppComponent implements AfterViewInit {
@ViewChild('videoPlayer') videoElementRef: ElementRef;
videoElement: HTMLVideoElement;
manifestUri = 'http://hlsliveamdgl7-lh.akamaihd.net/i/hlsdvrlive_1@583042/master.m3u8';
ngAfterViewInit() {
shaka.polyfill.installAll();
this.videoElement = this.videoElementRef.nativeElement;
this.initPlayer();
}
private initPlayer() {
shaka.media.ManifestParser.registerParserByExtension("m3u8", shaka.hls.HlsParser);
shaka.media.ManifestParser.registerParserByMime("Application/vnd.apple.mpegurl", shaka.hls.HlsParser);
shaka.media.ManifestParser.registerParserByMime("application/x-mpegURL", shaka.hls.HlsParser);
let player = new shaka.Player(this.videoElement);
player.load(this.manifestUri).then(() => {
// This runs if the asynchronous load is successful.
console.log('The video has now been loaded!');
}).catch(this.onError); // onError is executed if …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个以这种方式工作的树枝过滤器: {{entities|fieldnames}} 将返回一个包含实体对象属性名称的数组。我的问题是,在阅读并尝试了几个小时之后,我无法从 Twig Extension php 中执行 $this->container->get("helpers") 。似乎我没有正确链接服务容器......请帮助;)
错误:在 /Users/a77/Dropbox/06.Proyectos/2011 U-Vox/DEV U-Vox/Uvox Web/src/Acme/DemoBundle/Extension/FieldnamesTwigExtension 中的非对象上调用成员函数 get()。 php 第 38 行
或者如果构造没有 =null 错误 ContextErrorException: Catchable Fatal Error: Argument 1 passed to Acme\DemoBundle\Extension\FieldnamesTwigExtension::__construct() 必须是 Acme\DemoBundle\Extension\Container 的一个实例,没有给出,调用
服务.yml
服务:
Run Code Online (Sandbox Code Playgroud)helpers: class: Acme\DemoBundle\Services\Helpers twig.extension.acme.demo: class: Acme\DemoBundle\Twig\Extension\DemoExtension arguments: [twig.loader] acme.demo.listener: class: Acme\DemoBundle\EventListener\ControllerListener arguments: [twig.extension.acme.demo] fieldnames: class: Acme\DemoBundle\Extension\FieldnamesTwigExtension arguments: [@service_container]
扩展名\字段名TwigExtension.php
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
namespace Acme\DemoBundle\Extension;
class FieldnamesTwigExtension extends \Twig_Extension {
private $container;
public function __construct(Container $container=null)
{
$this->container = $container;
//var_dump ($container); …Run Code Online (Sandbox Code Playgroud)