A Complete Guide to Integrate Facebook Like and Share
Nowadays social media platform has grown a lot more and really helpful in marketing, sharing information, communication etc. So here in this post, I am going into detail about how to integrate the Facebook social plugin, with real-time Like, Share button on your products links or blog posts on your web application.
I will be using Laravel framework to demonstrate this integration. So before starting, I will assume you have laravel framework setup, facebook account ready to use in facebook developers mode.
At the end of the post, you will get the below output.
Now let's start with creating the basic foundation to show the blog posts and view it.
app/routes/web.php
Route::group(['prefix' => 'blog'], function () {
Route::get('/', [
'as' => 'app.blog',
'uses' => 'BlogController@index',
]);
Route::get('/{slug}', [
'as' => 'app.blog.view',
'uses' => 'BlogController@view',
]);
});
app/database/migrations
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateBlogsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('blogs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('slug');
$table->string('excerpt');
$table->longText('content')->nullable();
$table->timestamp('published_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('blogs');
}
}
app/Blog.php
<?php
namespace App;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
/**
* Class Blog
* @package App
*/
class Blog extends Model
{
/**
* @var string
*/
protected $table = 'blogs';
/**
* @var array
*/
protected $dates = ['published_at'];
/**
* @var array
*/
protected $fillable = ['title', 'slug', 'excerpt', 'content', 'published_at'];
/**
* @param $query
* @return mixed
*/
public function scopePublished($query)
{
return $query->where('published_at', '<=', Carbon::now());
}
/**
* @param $date
*/
public function setPublishedAtAttribute($date)
{
$this->attributes['published_at'] = Carbon::parse($date);
}
}
app/Http/Controllers/BlogController.php
<?php
namespace App\Http\Controllers;
use App\Blog;
use Illuminate\Http\Request;
/**
* Class BlogController
* @package App\Http\Controllers
*/
class BlogController extends Controller
{
/**
* @var int
*/
protected $paginate = 10;
/**
* @param Request $request
*/
public function __construct(Request $request)
{
$this->request = $request;
}
public function index()
{
$blogs = Blog::published()->paginate(
$this->paginate
);
return view('blog.index', [
'blogs' => $blogs,
'title' => 'Blogs',
'open_graph' => [
'title' => 'Blogs',
'image' => asset('assets/logo.jpeg'),
'url' => $this->request->url(),
'description' => 'A blog website to share tutorials and tips !',
'keywords' => 'A Laravel Blog, Tips, Tutorials',
],
]);
}
/**
* @param $slug
*/
public function view($slug)
{
$blog = Blog::published()
->where('slug', $slug)
->firstOrFail();
return view('blog.view', [
'blog' => $blog,
'open_graph' => [
'title' => $blog->title,
'image' => asset('assets/preview.jpeg'),
'url' => $this->request->url(),
'description' => $blog->excerpt,
],
]);
}
}
Now, I will assume you have following code in your layout blade in your view folder. See how I have created my layout file here for this tutorial.
<!DOCTYPE html>
<html lang="en">
<head>
....
....
....
@yield('head')
</head>
<body>
Open up the file form Github repository index.blade.php
Similarly, open up view.blade.php
The above integration will show the following output.
Blogs
Blog Preview
Now, let's navigate to facebook developers doc.
Scroll down to - Like Button Configurator
You are free to configure the URL to Like, Width, Layout, Action Type, Button Size, Show Friends's Faces, Include Share Button options as per your wish.
After completing the above configuration click on Get Code button. It will show a pop up window like below. Don't forget to read the information shown in the pop up window carefully. The share feature requires open graph meta tags on heads section of your html page, so have a look at the code to know how I implemented it.
There are two different way of implementing the facebook real-time like, share buttons.
Javascript SDK
Step 1: Include the JavaScript SDK on your page once, ideally right after the opening body
tag.
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = 'https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.11&appId=180539309049634';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
Don't forget to replace the appId=180539309049634
with your own app. Select your facebook app before generating the code. If you don't have any app for your site, create one from facebook developers page under My App.
Step 2: Place this code wherever you want the plugin to appear on your page.
<div class="fb-like" data-href="{{ route('app.blog.view', ['slug' => $post->slug]) }}" data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div>
IFrame
<iframe src="https://www.facebook.com/plugins/like.php?href={{ route('app.blog.view', ['slug' => $post->slug]) }}&width=51&layout=box_count&action=like&size=small&show_faces=true&share=true&height=65&appId" width="51" height="65" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>
Place this code wherever you want the plugin to appear on your page.
Now, look at the index.blade.php, view.blade.php on my Github repo about how I implemented the code to show the facebook real-time like, share buttons.
View the complete code pushed to github while I was writing this tutorial.
Finally! now we have a complete guide setup for integrating facebook, real-time like, share buttons.
Conclusion
Thanks for reading up to the end. If you have any feedback please leave your comments below. Feel free to share with friends if you like it.
Happy Coding!