2011-08-17 32 views
5

Tôi đang cố gắng không có may mắn để tìm đối tượng "liên kết giới thiệu" để sử dụng trong bộ điều khiển của tôi. Tôi mong đợi sẽ có một đối tượng tương tự như đối tượng yêu cầu với các tham số xác định các đối số _controller, _route và .Symfony2: Đối tượng liên kết giới thiệu tương tự như đối tượng Yêu cầu?

Những gì tôi đang cố gắng làm là một ngôn ngữ switcher hành động chuyển hướng người dùng đến cùng một trang bằng ngôn ngữ mới. Một cái gì đó dọc theo dòng :

public function switchLangAction($_locale) 
{ 
    $args = array(); 
    $newLang = ($_locale == 'en') ? 'fr' : 'en'; 

    // this is how I would have hoped to get a reference to the referrer request. 
    $referrer = $this->get('referrer'); 
    $referrerRoute = $referrer->parameters->get('_route'); 
    $args = $referrer->parameters->get('args'); // not sure how to get the route args out of the params either! 
    $args['_locale'] = $newLang; 

    $response = new RedirectResponse($this->generateUrl(
     $referrerRoute, 
     $args 
    )); 

    return $response; 
} 

Nó cũng có thể là có một cách khác để làm điều này - Tôi biết trong ray có là "redirect_to: trở lại" phương pháp ví dụ.

Mọi trợ giúp sẽ được đánh giá cao.

Trả lời

4

Tại sao không thay đổi ngôn ngữ trong phiên của người dùng?

Thứ nhất, xác định miền địa phương của bạn trong các bộ định tuyến

my_login_route: 
    pattern: /lang/{_locale} 
    defaults: { _controller: AcmeDemoBundle:Locale:changeLang } 
    requirements: 
     _locale: ^en|fr$ 

Sau đó, thiết lập các phiên

namespace Acme\DemoBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class LocaleController extends Controller 
{ 
    public function switchLangAction($_locale, Request $request) 
    { 
     $session = $request->getSession(); 
     $session->setLocale($_locale); 
     // ... some other possible actions 

     return $this->redirect($session->get('referrer')); 
    } 
} 

Trong tất cả các bộ điều khiển khác mà bạn nên đặt biến phiên mình bằng cách gọi

$session->set('referrer', $request->getRequestUri()); 

Bạn cũng có thể tạo trình xử lý sự kiện để đặt biến phiên cho mỗi p tuổi tự động.

+0

Ok, cảm ơn, Gilden! –

+0

Tôi đã tìm hiểu thêm về Symfony2 sau khi viết bài đăng này. Bạn có thể tìm thấy triển khai tốt hơn [ở đây] (http://stackoverflow.com/questions/7414243/symfony2-help-please-with-backward-uri-referrer-during-switching-locale/7423276#7423276). – gilden

0

Đó là điều khiển của tôi

lớp LocaleController mở rộng điều khiển {

public function indexAction() 
{ 
    if(null === $this->getRequest()->getLocale()){ 
     $locale = $this->getRequest()->getPreferredLanguage($this->getLocales()); 
     $this->getRequest()->setLocale($locale); 
    } 
    else{ 
     $locale = $this->getRequest()->getLocale(); 
    } 

    return $this->redirect($this->generateUrl('corebundle_main_index', array('_locale' => $locale))); 
} 

public function changeLocaleAction($_locale) 
{ 
    $request = $this->getRequest(); 
    $referer = $request->headers->get('referer'); 
    $locales = implode('|',$this->getLocales()); 
    $url = preg_replace('/\/('.$locales.')\//', '/'.$_locale.'/', $referer, 1); 
    return $this->redirect($url); 
} 

private function getLocales() 
{ 
    return array('ru', 'uk', 'en'); 
} 


/** 
* @Template() 
*/ 
public function changeLocaleTemplateAction() 
{ 
    return array(); 
} 

}

Các vấn đề liên quan