提交 65ddd5e2 编写于 作者: 伟伟权

94. BaseController use translator service

上级 2dd91881
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Contracts\Translation\TranslatorInterface;
class BaseController extends AbstractController
{
public function addFlashMessages(string $type, $message, $parameters = [], $domain = null, $locale = null)
{
// dd($this->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
......@@ -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()]);
}
......
......@@ -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);
......
Post: '文章'
ID: 'ID'
Title: '标题'
\ No newline at end of file
Title: '标题'
comment_submit_message: '%name%, 您的评论已提交。'
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册