Facebook Like按钮,一个对象,多个URL(区域设置) [英] Facebook Like button, one object, multiple URL (locale)

查看:76
本文介绍了Facebook Like按钮,一个对象,多个URL(区域设置)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web应用程序,其中一个对象可以根据locale参数具有多个URL,如下所示:

I have a web app where the same object can have multiple URL's depending on the locale parameter, like so:

http://domain.com/{_locale}/{id}/{slug}

在这些页面上有一个Like按钮,效果很好,但是问题是,由于URL不同,在每个语言环境中完成的Like都算作一个单独的OpenGraph对象.

I have a Like button on those pages, which works just fine, but the problem is that Likes done in each locale count as a separate OpenGraph object since the URL is different.

显而易见的解决方案是使用对象ID而不是仅使用Like按钮的href参数,但使用

The obvious solution would be to use an object id instead of just the href parameter of the Like button, but it seems like it may not be possible.

基本上,我需要一种方法来对这两个因素进行计数:

Basically, I need a way for a Like to count for both:

http://domain.com/fr/1/some-slug

http://domain.com/en/1/some-slug

因为它们毕竟都是同一个对象.

Because they are, after all, both the same object.

有什么想法吗?

推荐答案

我想我必须将其键入才能解决.这是我对Symfony2的解决方案.

I guess I had to type it out to figure it out. Here's my solution for Symfony2.

使它工作的方法是调整现有的localeAction.默认情况下,它是通过转到网站的根目录触发的,但是我对其进行了更改,以便它接受路由和参数:

The way I got it to work was to tweak my existing localeAction. By default, it was triggered by going to the root of the website, but I changed it so it accepts a route and parameters:

public function localeAction($route = 'home', $parameters = array())
{
    $this->getRequest()->setLocale($this->getRequest()->getPreferredLanguage(array('en', 'fr')));

    return $this->redirect($this->generateUrl($route, $parameters));
}

然后,我为Facebook创建了一个路由配置:

Then, I created a route configuration specifically for Facebook:

facebook_profile:
  resource: "@AppCoreBundle/Controller/FacebookController.php"
  prefix: /fb
  type: annotation

在该控制器中,我通过id查找对象,从数据库中获取该条目并将其转发到区域设置控制器,该控制器将负责检测用户的区域设置并进行相应的重定向.

In that controller I find the object by id, get the slug from the database and forward to the locale controller which will take care of detecting the user's locale and redirect accordingly.

/**
 * @Route("/profile/{id}", requirements={"id" = "\d+"}, name="fb-profile")
 */
public function profileAction($id)
{
    $dog = $this->getDogManager()->findById($id);

    return $this->forward('AppCoreBundle:Core:locale', array(
        'route' => 'profile',
        'parameters' => array(
            'id' => $id,
            'slug' => $dog->getSlug(),
        ),
    ));
}

最后,视图可以为like按钮使用新路线,如下所示:

Lastly, the view can use the new route for the like button like so:

<div class="fb-like" data-href="http://domain.com{{ path('fb-profile', {'id': dog.id}) }}" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false"></div>

这将导致以下href:http://domain.com/fb/profile/1

我发现像这样设置og:url元标记很重要:

I found out it is important to set the og:url meta tag like so:

<meta property="og:url" content="http://domain.com{{ path('profile', {'id': dog.id, 'slug': dog.slug}) }}">

这将导致以下content:http://domain.com/fr/profile/1/some-slug

请注意,这是真实的URL,如果我在此处使用Facebook路由,则 debugger 会发牢骚关于循环重定向.哦,按钮也不起作用.

Notice this is the real URL, if I used the Facebook route here, the debugger whined about circular redirection. Oh and the button didn't work either.

我本可以在Facebook路线中使用该弹头,但是如果弹头由于某种原因而发生变化,它将破坏该对象的所有现有顶",这显然不是理想的副作用.

I could have used the slug in the Facebook route, but if the slug changes for one reason or another, it would destroy all the existing Likes for this object, which is obviously not a desirable side effect.

就是这样!

http://domain.com/fr/1/some-slug

http://domain.com/en/1/some-slug

现在在Facebook眼中是同一对象.

Are now the same object in the eyes of Facebook.

这篇关于Facebook Like按钮,一个对象,多个URL(区域设置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆