我在新的 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”参数提供一个值。参数可以为空并且没有提供空值,没有提供默认值,或者因为在此参数之后有一个非可选参数。
配置:service.yaml
services:
_defaults:
autowire: true
autoconfigure: true
public: false
...
App\Controller\:
autowire: true
resource: '../src/Controller'
tags: ['controller.service_arguments']
翻译.yaml
framework:
default_locale: '%locale%'
translator:
paths:
- '%kernel.project_dir%/translations'
fallbacks: ['en']