gpt4 book ai didi

php - Symfony 3 将所有路由重定向到当前语言环境版本

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:53:01 24 4
gpt4 key购买 nike

我正在开发一个 symfony 应用程序,我的目标是无论用户在哪个页面上,它都会导航到该页面的语言环境版本。

例如,如果用户导航到“/”主页,它将重定向到“/en/”

如果他们在“/admin”页面上,它将重定向到“/en/admin”,以这种方式从路由设置 _locale 属性.

如果他们从用户浏览器访问/admin,它还需要确定语言环境,因为没有确定语言环境,所以它知道要重定向到哪个页面。

目前我的默认 Controller 如下所示,因为我正在测试。我正在使用开发模式和分析器来测试翻译工作是否正确。

enter image description here

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
* @Route("/{_locale}/", name="homepage_locale")
*/
public function indexAction(Request $request)
{
$translated = $this->get('translator')->trans('Symfony is great');

// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'translated' => $translated
]);
}
}

如果用户导航到那里,当前的方法将使用户保持在“/”,但我想让它重定向到“/en/”。这也适用于其他页面,例如/admin 或/somepath/pathagain/article1 (/en/admin ,/en/somepath/pathagain/article1)

我该怎么做?

我读过的引用资料没有帮助:

Symfony2 Use default locale in routing (one URL for one language)

Symfony2 default locale in routing

::更新::

我还没有解决我的问题,但我已经很接近了,并且学会了一些提高效率的技巧。

DefaultController.php

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{

/**
* @Route("/", name="home", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
* @Route("/{_locale}/", name="home_locale", requirements={"_locale" = "%app.locales%"})
*/
public function indexAction(Request $request)
{
$translated = $this->get('translator')->trans('Symfony is great');

// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'translated' => $translated
]);
}

/**
* @Route("/admin", name="admin", defaults={"_locale"="en"}, requirements={"_locale" = "%app.locales%"})
* @Route("/{_locale}/admin", name="admin_locale", requirements={"_locale" = "%app.locales%"})
*/
public function adminAction(Request $request)
{
$translated = $this->get('translator')->trans('Symfony is great');

// replace this example code with whatever you need
return $this->render('default/index.html.twig', [
'base_dir' => realpath($this->container->getParameter('kernel.root_dir').'/..'),
'translated' => $translated
]);
}
}
?>

配置.yml

imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en
app.locales: en|es|zh

framework:
#esi: ~
translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
#serializer: { enable_annotations: true }
templating:
engines: ['twig']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
save_path: "%kernel.root_dir%/../var/sessions/%kernel.environment%"
fragments: ~
http_method_override: true
assets: ~

# Twig Configuration
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"

# Doctrine Configuration
doctrine:
dbal:
driver: pdo_mysql
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"

orm:
auto_generate_proxy_classes: "%kernel.debug%"
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true

# Swiftmailer Configuration
swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
username: "%mailer_user%"
password: "%mailer_password%"
spool: { type: memory }

注意参数值 app.locales: en|es|zh。现在,如果我计划在未来支持更多语言环境,那么每当我创建路线时,我都可以引用这个值。对于那些好奇的人,这些路线依次是英语、西类牙语、中文。在注释中的 DefaultController 中,"%app.locales%" 是引用配置参数的部分。

我当前方法的问题是转到/admin,例如不会将用户重定向到/{browsers locale}/admin,这将是保持一切井井有条的更优雅的解决方案......但至少路由有效.仍在寻找更好的解决方案。

****更新****

我想我可能在这里找到了答案,作为给出的底部答案 ( Add locale and requirements to all routes - Symfony2 ),Athlan 的答案。只是不确定如何在 symfony 3 中实现它,因为他的指示对我来说不够清楚。

我认为这篇文章也可能有所帮助 ( http://symfony.com/doc/current/components/event_dispatcher/introduction.html )

最佳答案

我没有足够的声誉来向正确的解决方案添加评论。所以我要添加一个新答案

你可以像这样在 app/config/routing.yml 添加“prefix:/{_locale}”:

app:
resource: "@AppBundle/Controller/"
type: annotation
prefix: /{_locale}

所以你不需要将它添加到每个 Action 的每个路由中。对于以下步骤。非常感谢,它很完美。

关于php - Symfony 3 将所有路由重定向到当前语言环境版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34711082/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com