Hardcoding - плохая идея, и это возможно, но не из коробки, насколько я знаю. То, что я сделал, чтобы предоставить URL-адрес с одним и тем же путем и параметрами, но для другого языкового стандарта, заключался в создании пользовательского расширения ветви, чтобы это сделать.
Это расширение обеспечивает новую функцию твига, которая будет считывать текущий маршрут, текущие параметры, исключать частные параметры и генерировать один и тот же маршрут, но для другого языкового стандарта. Здесь расширение прут в вопросе:
<?php
namespace Acme\WebsiteBundle\Twig\Extension;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LocalizeRouteExtension extends \Twig_Extension
{
protected $request;
protected $router;
public function __construct(ContainerInterface $container)
{
$this->request = $container->get('request');
$this->router = $container->get('router');
}
public function getFunctions()
{
return array(
'localize_route' => new \Twig_Function_Method($this, 'executeLocalizeRoute', array()),
);
}
public function getName()
{
return 'localize_route_extension';
}
/**
* Execute the localize_route twig function. The function will return
* a localized route of the current uri retrieved from the request object.
*
* The function will replace the current locale in the route with the
* locale provided by the user via the twig function.
*
* Current uri: http://www.example.com/ru/current/path?with=query
*
* In Twig: {{ localize_route('en') }} => http://www.example.com/en/current/path?with=query
* {{ localize_route('fr') }} => http://www.example.com/fr/current/path?with=query
*
* @param mixed $parameters The parameters of the function
* @param string $name The name of the templating to render if needed
*
* @return Output a string representation of the current localized route
*/
public function executeLocalizeRoute($parameters = array(), $name = null)
{
$attributes = $this->request->attributes->all();
$query = $this->request->query->all();
$route = $attributes['_route'];
# This will add query parameters to attributes and filter out attributes starting with _
$attributes = array_merge($query, $this->filterPrivateKeys($attributes));
$attributes['_locale'] = $parameters !== null ? $parameters : \Locale::getDefault();
return $this->router->generate($route, $attributes);
}
/**
* This function will filter private keys from the attributes array. A
* private key is a key starting with an underscore (_). The filtered array is
* then returned.
*
* @param array $attributes The original attributes to filter out
* @return array The filtered array, the array withtout private keys
*/
private function filterPrivateKeys($attributes)
{
$filteredAttributes = array();
foreach ($attributes as $key => $value) {
if (!empty($key) && $key[0] != '_') {
$filteredAttributes[$key] = $value;
}
}
return $filteredAttributes;
}
}
Теперь вы можете включить это расширение прут, загрузив это определение службы либо с помощью вашего комплекта, либо непосредственно в файл config.yml
, расположенный под app/config
.
services:
acme.twig.extension:
class: Acme\WebsiteBundle\Twig\Extension\LocalizeRouteExtension
scope: request
arguments:
request: "@request"
router: "@router"
tags:
- { name: twig.extension }
И теперь вы можете сделать это в веточке, чтобы предложить другую версию текущей загруженной страницы:
<a id='englishLinkId' href="{{ localize_route('en') }}">
English
</a>
<a id='frenchLinkId' href="{{ localize_route('fr') }}">
Français
</a>
Надеется, что это помогает, и что это то, что вы ищете.
Edit:
Кажется, что это не представляется возможным, чтобы сузить область расширения прута непосредственно. Чтобы этого избежать, сначала добавьте контейнер зависимостей, а затем извлеките необходимые службы. Это должно быть отражено путем изменения определения конструктора расширения ветки и определения службы для расширения. Я отредактировал свой предыдущий ответ, проверил новое обновленное определение конструктора и новое определение службы.
Другим возможным решением было бы ввести вспомогательную службу, которая отвечает за локализацию маршрута. Эта вспомогательная служба должна находиться в области запроса. Фактически, это то, что у меня есть в моем коде. У меня есть служба RoutingHelper
, которая вводится в расширение моей ветки. Затем, в методе executeLocalizeRoute
, я использую свой помощник, чтобы выполнить тяжелую работу.
Сообщите мне, если все работает сейчас.
С уважением,
Matt
работает как шарм, спасибо миллиону человек! – Confidence