मेरे पास इसका पूरा समाधान है जिसे मैंने कुछ शोध के बाद खोजा। मेरा समाधान मानता है कि आप चाहते हैं कि हर मार्ग में इसके सामने एक लोकेल हो, यहां तक कि लॉगिन भी करें। इसे सिम्फनी 3 का समर्थन करने के लिए संशोधित किया गया है, लेकिन मेरा मानना है कि यह अभी भी 2 में काम करेगा।
यह संस्करण यह भी मानता है कि आप ब्राउजर लोकेल को डिफ़ॉल्ट लोकेल के रूप में उपयोग करना चाहते हैं यदि वे रूट/व्यवस्थापक जैसे मार्ग पर जाते हैं, लेकिन यदि वे/en/admin पर जाएं यह लोकेल का उपयोग करने के लिए पता चलेगा। उदाहरण के लिए यह नीचे # 2 मामला है।
उदाहरण के लिए
तो:
1. User Navigates To -> "/" -> (redirects) -> "/en/"
2. User Navigates To -> "/admin" -> (redirects) -> "/en/admin"
3. User Navigates To -> "/en/admin" -> (no redirects) -> "/en/admin"
सभी स्थितियों स्थान सही ढंग से स्थापित किया जाएगा कि कैसे आप इसे अपने कार्यक्रम के दौरान इस्तेमाल के लिए चाहते हैं।
पूर्ण समाधान देख सकते हैं जो इसे लॉगिन और सुरक्षा के साथ काम करने के लिए कैसे, नहीं तो कम संस्करण शायद आप के लिए काम करेंगे शामिल नीचे:
Full Version
Symfony 3 Redirect All Routes To Current Locale Version
लघु संस्करण
ऐसा करने के लिए ताकि मेरे उदाहरणों में मामला # 2 संभव हो का उपयोग कर एक httpKernal listner
LocaleRewriteListener.php
<?php
//src/AppBundle/EventListener/LocaleRewriteListener.php
namespace AppBundle\EventListener;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Routing\RouteCollection;
class LocaleRewriteListener implements EventSubscriberInterface
{
/**
* @var Symfony\Component\Routing\RouterInterface
*/
private $router;
/**
* @var routeCollection \Symfony\Component\Routing\RouteCollection
*/
private $routeCollection;
/**
* @var string
*/
private $defaultLocale;
/**
* @var array
*/
private $supportedLocales;
/**
* @var string
*/
private $localeRouteParam;
public function __construct(RouterInterface $router, $defaultLocale = 'en', array $supportedLocales = array('en'), $localeRouteParam = '_locale')
{
$this->router = $router;
$this->routeCollection = $router->getRouteCollection();
$this->defaultLocale = $defaultLocale;
$this->supportedLocales = $supportedLocales;
$this->localeRouteParam = $localeRouteParam;
}
public function isLocaleSupported($locale)
{
return in_array($locale, $this->supportedLocales);
}
public function onKernelRequest(GetResponseEvent $event)
{
//GOAL:
// Redirect all incoming requests to their /locale/route equivlent as long as the route will exists when we do so.
// Do nothing if it already has /locale/ in the route to prevent redirect loops
$request = $event->getRequest();
$path = $request->getPathInfo();
$route_exists = false; //by default assume route does not exist.
foreach($this->routeCollection as $routeObject){
$routePath = $routeObject->getPath();
if($routePath == "/{_locale}".$path){
$route_exists = true;
break;
}
}
//If the route does indeed exist then lets redirect there.
if($route_exists == true){
//Get the locale from the users browser.
$locale = $request->getPreferredLanguage();
//If no locale from browser or locale not in list of known locales supported then set to defaultLocale set in config.yml
if($locale=="" || $this->isLocaleSupported($locale)==false){
$locale = $request->getDefaultLocale();
}
$event->setResponse(new RedirectResponse("/".$locale.$path));
}
//Otherwise do nothing and continue on~
}
public static function getSubscribedEvents()
{
return array(
// must be registered before the default Locale listener
KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
);
}
}
को समझने के लिए कि कैसे सिम्फोनी प्रलेखन पर घटना ग्राहक इंटरफ़ेस को देखने के काम कर रहा है ऐसा करने के लिए की जरूरत है।
listner आप इसे अपने services.yml में स्थापित करने की जरूरत है सक्रिय करने के लिए
services.yml
# Learn more about services, parameters and containers at
# http://symfony.com/doc/current/book/service_container.html
parameters:
# parameter_name: value
services:
# service_name:
# class: AppBundle\Directory\ClassName
# arguments: ["@another_service_name", "plain_value", "%parameter_name%"]
appBundle.eventListeners.localeRewriteListener:
class: AppBundle\EventListener\LocaleRewriteListener
arguments: ["@router", "%kernel.default_locale%", "%locale_supported%"]
tags:
- { name: kernel.event_subscriber }
अंत में इस चर अपने config.yml में परिभाषित करने की आवश्यकता है को संदर्भित करता है
config.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
locale_supported: ['en','es','zh']
अंत में, आपको यह सुनिश्चित करना होगा कि आपके सभी मार्ग अब के लिए/{locale} से शुरू हो जाएं। इस का एक नमूना मेरा डिफ़ॉल्ट controller.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
/**
* @Route("/{_locale}", requirements={"_locale" = "%app.locales%"})
*/
class DefaultController extends Controller
{
/**
* @Route("/", name="home")
*/
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")
*/
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
]);
}
}
?>
नोट आवश्यकताओं requirements={"_locale" = "%app.locales%"}
, इस config.yml फ़ाइल संदर्भित कर रहा है ताकि आप केवल सभी मार्गों के लिए एक ही स्थान पर उन आवश्यकताओं को परिभाषित करने के लिए है में नीचे है।
आशा इस मदद करता है किसी को :)
आप इस का समाधान कर सकता है? –
मैंने अभी तक अपनी वेबसाइट में डिफ़ॉल्ट स्थानीय के संबंध में कोई समाधान लागू नहीं किया है। मुझे जल्द ही ऐसा करना पड़ सकता है क्योंकि हम अंतरराष्ट्रीय जा रहे हैं, मैं उस समाधान की रिपोर्ट करूंगा जो हम यहां लागू करेंगे। मैंने अभी जवाब पढ़े हैं और @mattexx एक सबसे अच्छा/क्लीनर लगता है। मुझे नहीं पता था कि आप एक नियंत्रक के लिए एकाधिक मार्ग परिभाषित कर सकते हैं। –