laravel 如何在laravel中将当前网址分享到facebook?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38737953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:14:41  来源:igfitidea点击:

how to share the current url to facebook in laravel?

phpfacebooklaravellaravel-5

提问by Naveen Kumar

I want to share the current URL of my website to the facebook , so i used facebook's sharer.php. Here is my share.php file which shows icons and link to the sharer.php

我想将我网站的当前 URL 分享到 facebook,所以我使用了 facebook 的 sharer.php。这是我的 share.php 文件,它显示了图标和指向 sharer.php 的链接

<div class="social-buttons">
    <a href="https://www.facebook.com/sharer/sharer.php?u={{ urlencode($url) }}"
       target="_blank">
       <i class="fa fa-facebook-official"></i>
    </a>
</div>

I am passing the url to the ($url) from my page through the code

我正在通过代码将 url 从我的页面传递到 ($url)

<?php

use App\Http\Requests;
use Illuminate\Http\Request;

?>
@include('share',['url' => 'www.google.com'])

This particular webpage i am sharing the URL from usually generate dynamic URL. So I change the code to

我共享的这个特定网页的 URL 通常会生成动态 URL。所以我把代码改成

@include('share',['url' => 'Request::url()'])

so that i can share the dynamically created URL. The problem here is When i put www.google.comin place of url the POP UP box works fine and open up the facebook share. But when i use ['url' => 'Request::url()'])the pop up box opens and close quickly , i tried to see the url and it was something like error. How do i fix this? is there any drawback in this approach ?

这样我就可以共享动态创建的 URL。这里的问题是当我www.google.com代替 url 时,POP UP 框工作正常并打开 facebook 共享。但是当我使用['url' => 'Request::url()'])弹出框快速打开和关闭时,我试图查看 url 并且它是类似错误的东西。我该如何解决?这种方法有什么缺点吗?

Here is the script code

这是脚本代码

<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script>

    var popupSize = {
        width: 780,
        height: 550
    };

    $(document).on('click', '.social-buttons > a', function(e){

        var
            verticalPos = Math.floor(($(window).width() - popupSize.width) / 2),
            horisontalPos = Math.floor(($(window).height() - popupSize.height) / 2);

        var popup = window.open($(this).prop('href'), 'social',
            'width='+popupSize.width+',height='+popupSize.height+
            ',left='+verticalPos+',top='+horisontalPos+
            ',location=0,menubar=0,toolbar=0,status=0,scrollbars=1,resizable=1');

        if (popup) {
            popup.focus();
            e.preventDefault();
        }

    });
</script>

回答by jedrzej.kurylo

When you do

当你做

@include('share',['url' => 'Request::url()'])

you're passing a string Request::url()as the value, not the URL.

您传递的是字符串Request::url()作为值,而不是 URL。

Replace that line with:

将该行替换为:

@include('share',['url' => Request::url()])