Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Share URL on social media using package in Laravel 98765

How to Share URL on social media using package in Laravel 9/8/7/6/5 ?

Aman Jain, March 15, 2022October 4, 2022

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

  • Facebook
  • Twitter
  • LinkedIn
  • WhatsApp
  • Reddit
  • Telegram
Screenshot 2022 03 15 at 10.27.58 PM
Share URL on social media using package in Laravel

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();

Related

Php Laravel Laravel 9 facebooklaravellinkdinphpsharetwitterwhatsapp

Post navigation

Previous post
Next post

Related Posts

Php Show Validation Error Message

How to Show Validation Error Message in Laravel ?

November 10, 2023March 16, 2024

In this article i will explain you to show validation error message in laravel. One crucial aspect of web development is handling form validation and displaying error messages to users effectively. In this guide, we will delve into the intricacies of showing validation error messages in Laravel, ensuring a smooth…

Read More
Php Simplest Way to Install Laravel 10

Simplest Way to Install Laravel 10

March 12, 2023March 16, 2024

Laravel is most popular framework of PHP. It provides lots of in-build tools and library to build the web application. laravel community has working quite hard to make the laravel most efficient and secured framework for web developers that’s why most of the websites are built with laravel as backend….

Read More

Sharing variables between all views in laravel 8

January 15, 2022January 15, 2022

In laravel its easy to share varible from controller to view using view function but sometimes we need to share some specific information to all pages of our website for examle sharing the site settings, user info etc. so in this case we require to share a varible across the…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version