问题
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}
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.
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
and
http://domain.com/en/1/some-slug
Because they are, after all, both the same object.
Any ideas?
回答1:
I guess I had to type it out to figure it out. Here's my solution for Symfony2.
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));
}
Then, I created a route configuration specifically for Facebook:
facebook_profile:
resource: "@AppCoreBundle/Controller/FacebookController.php"
prefix: /fb
type: annotation
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(),
),
));
}
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>
Which results in the following href
: http://domain.com/fb/profile/1
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}) }}">
Which results in the following content
: http://domain.com/fr/profile/1/some-slug
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.
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.
That's it!
http://domain.com/fr/1/some-slug
and
http://domain.com/en/1/some-slug
Are now the same object in the eyes of Facebook.
来源:https://stackoverflow.com/questions/16764485/facebook-like-button-one-object-multiple-url-locale