我正在开发一个 symfony 应用程序,我的目标是无论用户在哪个页面上,它都会导航到页面的区域设置版本。
例如,如果用户导航到“/”主页,它将重定向到“/en/”
如果他们在 "/admin" 页面上,它将重定向到 "/en/admin",这样_locale
属性是从路由设置的。
如果他们从用户浏览器访问/admin,它还需要确定语言环境,因为没有确定语言环境,因此它知道要重定向到哪个页面。
目前我的默认控制器如下所示,因为我正在测试。我正在使用开发模式和分析器来测试翻译是否正确。
<?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 在路由中使用默认语言环境(一种语言的一个 URL)
::更新::
我还没有解决我的问题,但我已经接近并学习了一些提高效率的技巧。
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,这将是保持一切井井有条的更优雅的解决方案......但至少路由有效。仍在寻找更好的解决方案。
****更新****
我想我可能在这里找到了答案作为给出的底部答案(向所有路线添加区域设置和要求 - Symfony2),Athlan 的答案。只是不确定如何在 symfony 3 中实现这一点,因为他的指示对我来说不够清楚。
我认为这篇文章也可能有所帮助(http://symfony.com/doc/current/components/event_dispatcher/introduction.html)