提交 eb9992b3 编写于 作者: 伟伟权

96. {_locale} in url

上级 b9cdff1d
......@@ -14,6 +14,7 @@ parameters:
- 'hello'
- 'world'
- 'abc'
support_locales: en|zh_CN|fr
services:
# default configuration for services in *this* file
_defaults:
......
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class HomePageController extends AbstractController
{
#[Route('/', name: 'home_page')]
public function index(): Response
{
return $this->redirectToRoute('post_index', [
'_locale' => 'zh_CN'
]);
}
}
......@@ -22,10 +22,13 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/{_locale<%support_locales%>}')]
class PostController extends BaseController
{
#[Route('/', name: 'post_index', methods: ['GET'])]
#[Route('/', name: 'post_index',
// requirements: ['_locale'=> 'en|zh_CN'],
methods: ['GET']
)]
public function index(Request $request, PostRepository $postRepository, Security $security): Response
{
// $user = $this->getUser();
......
{% extends 'base.html.twig' %}
{% block title %}Hello HomePageController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code><a href="{{ '/Users/quanweiwei/teebblog/src/Controller/HomePageController.php'|file_link(0) }}">src/Controller/HomePageController.php</a></code></li>
<li>Your template at <code><a href="{{ '/Users/quanweiwei/teebblog/templates/home_page/index.html.twig'|file_link(0) }}">templates/home_page/index.html.twig</a></code></li>
</ul>
</div>
{% endblock %}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册