In this tutorial i will show you to Share URL on social media using package in Laravel. Laravel provides several packages to make the task easy, In the same way we can add third party package to share the URLs on multiple social media using the package jorenvanhocht/laravel-share
.
This package allow us to share the url on multiple platform as below
- Telegram
jorenvanhocht/laravel-share
provides multiple option configure the package and it also have some dependencies like fontawesome
and jQuery.
Sharing a url can be a requirements of each and every page, and it can be a pain if we are doing it manually so using this package we can make easy.
So let’s begin the tutorial Share URL on social media using package in Laravel step by step
Step 1 : Install the package
I assume the you have already installed the laravel and basic connection of it like database connection and composer.
Now install the package using composer in laravel root directory, open the terminal in laravel root directory and run below command
composer require jorenvanhocht/laravel-share
And then run composer update
composer update
Step 2 : Add Service provider and alias
Most of the packages comes with laravel package discovery enabled and automatic add service provider to packages built after laravel 5.5
so if your laravel version is greater then 5.5
then you can skip this step.
Search for providers
key and Add service provider in config\app.php
'providers' => [
// ...
Jorenvh\Share\Providers\ShareServiceProvider::class,
]
Also find alias key and add as below
aliases' => [
// ...
'Share' => Jorenvh\Share\ShareFacade::class,
]
Step 3 : Publish config to add configuration
Now, publish the config of package so we can change the config according to need. Run the below command in project directory
php artisan vendor:publish --provider="Jorenvh\Share\Providers\ShareServiceProvider"
This will publish the laravel-share.php
config file to your config folder, share.js
in public/js/
and laravel-share.php
in your resources/lang/vendor/en/
folder
<?php
return [
/*
|--------------------------------------------------------------------------
| Services
|--------------------------------------------------------------------------
|
| Specify the base uri for each service.
|
|
|
*/
'services' => [
'facebook' => [
'uri' => 'https://www.facebook.com/sharer/sharer.php?u=',
],
'twitter' => [
'uri' => 'https://twitter.com/intent/tweet',
'text' => 'Default share text',
],
'linkedin' => [
'uri' => 'https://www.linkedin.com/sharing/share-offsite', // oud: http://www.linkedin.com/shareArticle
'extra' => ['mini' => 'true'],
],
'whatsapp' => [
'uri' => 'https://wa.me/?text=',
'extra' => ['mini' => 'true'],
],
'pinterest' => [
'uri' => 'https://pinterest.com/pin/create/button/?url=',
],
'reddit' => [
'uri' => 'https://www.reddit.com/submit',
'text' => 'Default share text',
],
'telegram' => [
'uri' => 'https://telegram.me/share/url',
'text' => 'Default share text',
],
],
/*
|--------------------------------------------------------------------------
| Font Awesome
|--------------------------------------------------------------------------
|
| Specify the version of Font Awesome that you want to use.
| We support version 4 and 5.
|
|
*/
'fontAwesomeVersion' => 5,
];
Step 4 : Create routes
Add appropriate routes to connect our URL to controller therefore open routes/web.php
and add below routes
<?php
use Illuminate\Support\Facades\Route;
use \App\Http\Controllers\PostController;
Route::get('/share-post',[PostController::class, 'share']);
Here we created one route only that means you can create it anywhere but we are using it only our demo purpose.
Step 4 : Create Controller
Now, create the controller as we have mentioned in routes is PostController
and also create three methods create
, store
and refreshCaptcha
so create a file in app\Http\Controllers\PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class PostController extends Controller
{
public function share()
{
return view('post.sahre-post');
}
}
Step 5 : Create view
We have create the controller and now we will create the view to show the button for sharing on social media
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Readerstacks Share URL on social media like Facebook, Whatsapp, Twitter, LinkedIn in Laravel </title>
<link href="//netdna.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.10.1/css/all.min.css" integrity="sha512-9my9Mb2+0YO+I4PUCSwUYO7sEK21Y0STBAiFEYoWtd2VzLEZZ4QARDrZ30hdM1GlioHJ8o8cWQiy8IAb1hy/Hg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
<body class="antialiased">
<div class="container">
<!-- main app container -->
<div class="readersack">
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<h3> Share URL on social media like Facebook, Whatsapp, Twitter, LinkedIn in Laravel - Readerstacks</h3>
{!!Share::page('https://readerstacks.com', 'Share title',["target"=>"_blank"])
->facebook()
->twitter()
->linkedin('Extra linkedin summary can be passed here')
->whatsapp();
!!}
</div>
</div>
</div>
</div>
<!-- credits -->
<div class="text-center">
<p>
<a href="#" target="_top"> Share URL on social media like Facebook, Whatsapp, Twitter, LinkedIn in Laravel - Readerstacks</a>
</p>
<p>
<a href="https://readerstacks.com" target="_top">readerstacks.com</a>
</p>
</div>
</div>
</body>
</html>
Here, we used {!! {!!Share::page() !!} to show the social media icons
{!!Share::page('https://readerstacks.com', 'Share title',["class"=>"social"])
->facebook()
->twitter()
->linkedin('Extra linkedin summary can be passed here')
->whatsapp();
!!}
Open share url in new tab or popup
you can open the share url in new tab or in window popup using including script file and jQuery in page as below
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<script src="{{ asset('js/share.js') }}"></script>
Now serve the project using
php artisan serve
and check the implementation.
Creating Facebook share link only
Share::page('http://jorenvanhocht.be')->facebook();
Also Read : How to Share Page to Facebook in Laravel PHP ?
Creating Twitter share link only
Share::page('http://jorenvanhocht.be', 'Your share text can be placed here')->twitter();
Creating Reddit share link only
Share::page('http://jorenvanhocht.be', 'Your share text can be placed here')->reddit();
Creating LinkedIn share link only
Share::page('http://jorenvanhocht.be', 'Share title')->linkedin('Extra linkedin summary can be passed here')
Creating WhatsApp share link only
Share::page('http://jorenvanhocht.be')->whatsapp()
Also Read : How to Share/Send Message to WhatsApp in Laravel PHP ?
Creating Telegram share link only
Share::page('http://jorenvanhocht.be', 'Your share text can be placed here')->telegram();
Sharing the current url
Instead of manually passing an url, you can opt to use the currentPage
function.
Share::currentPage()->facebook();