i18n问题 - 没有名称check-twitter和check-facebook的资源所有者

时间:2013-10-03 09:33:28

标签: facebook symfony twitter internationalization hwioauthbundle

我使用这些捆绑

https://github.com/hwi/HWIOAuthBundle
bundle:JMSI18nRoutingBundle
https://gist.github.com/danvbe/4476697

插入i18n后我的路线是

it__RG__hwi_oauth_connect                 ANY      ANY    ANY  /it/login/
en__RG__hwi_oauth_connect                 ANY      ANY    ANY  /en/login/
it__RG__hwi_oauth_connect_service         ANY      ANY    ANY  /it/login/service/{service}
en__RG__hwi_oauth_connect_service         ANY      ANY    ANY  /en/login/service/{service}
it__RG__hwi_oauth_connect_registration    ANY      ANY    ANY  /it/login/registration/{key}
en__RG__hwi_oauth_connect_registration    ANY      ANY    ANY  /en/login/registration/{key}
it__RG__hwi_oauth_service_redirect        ANY      ANY    ANY  /it/login/{service}
en__RG__hwi_oauth_service_redirect        ANY      ANY    ANY  /en/login/{service}
it__RG__facebook_login                    ANY      ANY    ANY  /it/login/check-facebook
en__RG__facebook_login                    ANY      ANY    ANY  /en/login/check-facebook
it__RG__twitter_login                     ANY      ANY    ANY  /it/login/check-twitter
en__RG__twitter_login                     ANY      ANY    ANY  /en/login/check-twitter

但是当我尝试去这里时我有这个错误:“/ en / login / check-facebook”(或重定向facebook或twitter后我有此错误)

No resource owner with name 'check-facebook'.

/app/config/routing.yml

#HWIOAuthBundle routes
hwi_oauth_security:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix: /login

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix: /login

hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /login

facebook_login:
    pattern: /login/check-facebook

twitter_login:
    pattern: /login/check-twitter

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,不是最佳解决方案,但有效

在我的主页

HomepageController

public function indexAction()
{
    $request = $this->get('request');
    /**
     * Store it into session the user language
     */
    $sessionId  = $this->get("session");
    if($sessionId->get("lingua")==""){
        $this->get('session')->set('lingua', $request->getLocale());
    }

    return array();

}

在另一个已被覆盖的FOSUserBundle包中

UserBundle / Controller / RegistrationController.php(例如)


public function customAction() {

    /**
     * Store it into session the user language
     */
    $sessionId  = $this->container->get("session");
    if($sessionId->get("lingua")==""){
        $this->container->get("session")->set('lingua',  $this->container->get('request')->getLocale());
    }

    return array(

    );
}

然后我创建了一个服务:http://symfony.com/doc/current/cookbook/session/locale_sticky_session.html

的services.xml

services:
    acme_locale.locale_listener:
        class: Acme\LocaleBundle\EventListener\LocaleListener
        arguments: ["%kernel.default_locale%",@session]
        tags:
            - { name: kernel.event_subscriber }

LocaleListener



namespace Acme\LocaleBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session;


class SnLocaleListener implements EventSubscriberInterface
{
    private $defaultLocale;
    private $session;


    public function __construct($defaultLocale = 'it', $session)
    {
        $this->defaultLocale = $defaultLocale;
        $this->session = $session;
    }

    public function onKernelRequest(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        if (!$request->hasPreviousSession()) {
            return;
        }

        //incapsula la lingua in sessione se esiste
        $locale_session = $this->session->get('lingua');

        if ($locale_session == "") {

            // prova a vedere se il locale sia stato impostato come parametro _locale di una rotta
            if ($locale = $request->attributes->get('_locale')) {
                $request->getSession()->set('_locale', $locale);
            } else {
                // se non trova un locale esplicito in questa richiesta, usa quello della sessione
                $request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
            }

        } else {
            $request->setLocale($request->getSession()->get('_locale', $locale_session));
            $request->getSession()->set('_locale', $locale_session);
            $request->attributes->set('_locale', $locale_session);
        }


    }

    public static function getSubscribedEvents()
    {
        return array(
            // deve essere registrato prima dell'ascoltatore predefinito di locale
            KernelEvents::REQUEST => array(array('onKernelRequest', 17)),
        );
    }
}

/app/config/security.yml

..
oauth:
                remember_me: true
                resource_owners:
                    facebook:           "/%locale%/login/check-facebook"
                    twitter:            "/%locale%/login/check-twitter"
                login_path:        fos_user_security_ripeti_login
                failure_path:      fos_user_security_ripeti_login
..
抱歉,我的英语不完美.. 对我来说这个解决方案有效,它不会是最好的