From 65ddd5e2bdc8b2d626793f64fd7710e51bedb953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=9F=E4=BC=9F=E6=9D=83?= Date: Sat, 24 Jul 2021 19:49:36 +0800 Subject: [PATCH] 94. BaseController use translator service --- src/Controller/BaseController.php | 25 +++++++++++++++++++++++++ src/Controller/CommentController.php | 5 +++-- src/Controller/PostController.php | 8 ++++++-- translations/messages.zh_CN.yaml | 3 ++- 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/Controller/BaseController.php diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php new file mode 100644 index 0000000..df53691 --- /dev/null +++ b/src/Controller/BaseController.php @@ -0,0 +1,25 @@ +container); + $translator = $this->container->get('translator'); + $this->addFlash($type, $translator->trans($message, $parameters, $domain, $locale)); + } + + public static function getSubscribedServices() + { + return array_merge(parent::getSubscribedServices(), [ + 'translator' => '?'.TranslatorInterface::class + ]); + } +} \ No newline at end of file diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index db668fc..32dadfd 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -class CommentController extends AbstractController +class CommentController extends BaseController { #[ Route('/post/{post_id}/comment/{comment_id}/reply',options: ['expose' => true], name: 'reply_comment'), @@ -66,7 +66,8 @@ class CommentController extends AbstractController $em->persist($data); $em->flush(); - $this->addFlash('success', '您的评论已成功提交!'); +// $this->addFlash('success', '您的评论已成功提交!'); + $this->addFlashMessages('success', 'comment_submit_message'); return $this->redirectToRoute('post_show', ['id1' => $post->getId()]); } diff --git a/src/Controller/PostController.php b/src/Controller/PostController.php index cd860ab..23905ff 100644 --- a/src/Controller/PostController.php +++ b/src/Controller/PostController.php @@ -20,9 +20,10 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Security; +use Symfony\Contracts\Translation\TranslatorInterface; -class PostController extends AbstractController +class PostController extends BaseController { #[Route('/', name: 'post_index', methods: ['GET'])] public function index(Request $request, PostRepository $postRepository, Security $security): Response @@ -47,6 +48,7 @@ class PostController extends AbstractController #[ParamConverter('post', options: ['id' => 'id1'])] public function show(Request $request, Post $post, EntityManagerInterface $entityManager, PaginatorInterface $paginator, CommentRepository $commentRepository, + TranslatorInterface $translator, EventDispatcherInterface $eventDispatcher): Response { $commentForm = $this->createForm(CommentType::class); @@ -88,9 +90,11 @@ class PostController extends AbstractController $data->setPost($post); $entityManager->persist($data); $entityManager->flush(); + + $this->addFlashMessages('success', 'comment_submit_message',['%name%'=>$data->getAuthor()]); +// $this->addFlash('success', $translator->trans('comment_submit_message')); } - $this->addFlash('success', '您的评论已成功提交!'); } $query = $commentRepository->getPaginationQuery($post); diff --git a/translations/messages.zh_CN.yaml b/translations/messages.zh_CN.yaml index 854d382..1b1f19e 100644 --- a/translations/messages.zh_CN.yaml +++ b/translations/messages.zh_CN.yaml @@ -1,3 +1,4 @@ Post: '文章' ID: 'ID' -Title: '标题' \ No newline at end of file +Title: '标题' +comment_submit_message: '%name%, 您的评论已提交。' \ No newline at end of file -- GitLab