在中间件中检测Facebook Crawler [英] Detect Facebook Crawler in middleware

查看:107
本文介绍了在中间件中检测Facebook Crawler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的laravel应用程序中有几个页面,我需要在Facebook中共享链接.

I have several pages in my laravel app that I need to share link in Facebook.

事情是,当我共享它时,Facebook从登录页面获取了og:image属性,因为它是由我的中间件重定向的.

Thing is, when I share it, Facebook get the properties og:image from login page, as it is redirect by my middleware.

我在此处看到必须检测Facebook用户代理和重定向到仅具有openGraph属性以在Facebook中呈现链接的公共页面.

I saw here that I must detect Facebook User Agent and redirect to a public page that should only have the openGraph properties to render link in Facebook.

这是唯一的方法吗?

我最终将创建一个内部软件,其中带有一个开关,用于管理我拥有的所有公共公开页面,这不是很好...

I will end up creating a middleware, with a switch inside, to manage all the differents publics pages I have, it is not so nice...

任何想法我该怎么做?

推荐答案

我通过创建自己的中间件解决了它:RedirectCrawlers

I solved it creating my own Middleware : RedirectCrawlers

    <?php

namespace App\Http\Middleware;

use App\Tournament;
use Closure;
use Illuminate\Support\Facades\Route;

class RedirectCrawlers
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $crawlers = [
            'facebookexternalhit/1.1',
            'facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)',
            'Facebot',
            'Twitterbot',
        ];

        $userAgent = $request->header('User-Agent');

        if (str_contains($userAgent, $crawlers)) {
            switch (Route::getCurrentRoute()->getPath()) {
                case "tournaments/{tournament}/register":
                    $tournament = Tournament::where('slug', $request->tournament)->first();
                    return view('public/register', compact('tournament'));
            }
        }
        return $next($request);
    }
}

然后我创建了一个resources/views/public/register.blade.php

Then I created a resources/views/public/register.blade.php

<meta property="og:title" content="{{trans('core.competitors_register') }}"/>


<meta name="description" content="Registrate en el torneo {{ $tournament->name }}"/>
<meta property="og:description" content="Registrate en el torneo {{ $tournament->name }}"/>
<meta property="og:type" content="website"/>
<meta property="og:image" content="https://kendozone.com/wp-content/uploads/2016/04/home.jpg"/>
<meta property="og:image:secure_url" content="https://kendozone.com/wp-content/uploads/2016/04/home.jpg"/>
<meta property="og:url" content="{{$request->url()}}"/>
<meta property="fb:app_id" content="780774498699579"/>

<meta name=" twitter:title" content="{{trans('core.competitors_register')}}"/>
<meta name="twitter:image" content="https://kendozone.com/wp-content/uploads/2016/04/home.jpg"/>
<meta name="twitter:card" content="summary"/>
<meta name="twitter:description" content="Registrate en el torneo {{ $tournament->name }}"/>

这是一个公共页面,仅具有共享到Facebook所需的信息.

that is a public page, that just have info needed to share to Facebook.

然后在App \ Http \ Kernel.php中,在验证中间件之前,将路由添加到"web"中的$ middlewareGroups内,以便将其重定向到公共页面:

Then in App\Http\Kernel.php I add the route inside $middlewareGroups in 'web', before Authenticate Middleware, so that it redirect it to the public page :

        \App\Http\Middleware\RedirectCrawlers::class,

解决方案存在于stackOverflow中,如所评论的那样,但对于Laravel 5.3而言,都不存在!

Solution exists in stackOverflow, like commented, but none of them for Laravel 5.3!

希望有帮助!

这篇关于在中间件中检测Facebook Crawler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆