我在新的symfony 4中遇到了问题。
<?php
namespace App\Controller;
use App\Entity\Flight;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Form\FlightType;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
/**
* Class DefaultController
* @package App\Controller
*/
class DefaultController extends Controller
{
/**
*
* @Route("/")
* @Route("/{_locale}/", name="homepage", requirements={"_locale" = "%app.locales%"})
*
* @param Translator $translator
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function index(Translator $translator, Request $request)
{
$translated = $translator->trans('Symfony is great');
错误: 控制器“App \ Controller \ DefaultController :: index()”要求您为“$ translator”参数提供值。参数可以为空,并且没有提供空值,没有提供默认值,或者因为在此之后存在非可选参数。
CONFIGS: service.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
...
App\Controller\:
autowire: true
resource: '../src/Controller'
tags: ['controller.service_arguments']
translation.yaml
framework:
default_locale: '%locale%'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks: ['en']
什么错了?手册在这里: http://symfony.com/doc/current/translation.html
答案 0 :(得分:2)
找到答案。