带有 Laravel 的 Google AMP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39707938/
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:30:40  来源:igfitidea点击:

Google AMP with Laravel

laravellaravel-bladeamp-html

提问by Genius in trouble

I have recently launched an ecommerce site based on laravel. And now i really think its a good idea to implement AMP as it supports ecommerce now (even shopify and ebay are implementing it)

我最近推出了一个基于 laravel 的电子商务网站。现在我真的认为实施 AMP 是个好主意,因为它现在支持电子商务(甚至 shopify 和 ebay 也在实施它)

So my query is how can we implement AMP on laravel? Is there a way to use desktopMainTempalte.blade.php for desktop version and switch to mobileMainTemplate.blade.php on mobile version? I just don't want to create a different domain for mobile device as m.domain.com. I want something creative here but Im not sure if i am going in the right direction.

所以我的问题是我们如何在 laravel 上实现 AMP?有没有办法在桌面版上使用 desktopMainTempalte.blade.php 而在移动版上切换到 mobileMainTemplate.blade.php?我只是不想为移动设备创建一个不同的域作为 m.domain.com。我想要一些有创意的东西,但我不确定我是否朝着正确的方向前进。

What would you guys be doing if you were in my shoe?

如果你在我的鞋子里,你们会做什么?

采纳答案by Mahdyfo

Using different routes and controllers to do the same but with a different view is very difficult. You can do this for a better approach.

使用不同的路由和控制器来做同样的事情但使用不同的视图是非常困难的。您可以这样做以获得更好的方法。

  1. Use the same web controllers for /amp/urls
  2. View the amp pages differently by modifying view()helper
  1. /amp/url使用相同的 Web 控制器
  2. 通过修改view()助手以不同的方式查看放大器页面

Amp Routes

放大器路线

To catch the /amp/routes, add this to your RouteServiceProvider.php

要捕捉/amp/路线,请将其添加到您的RouteServiceProvider.php

protected function mapAmpRoutes()
{
    Route::group([
        'middleware' => 'web',
        'namespace' => $this->namespace,
        'prefix' => 'amp',
    ], function ($router) {
        require base_path('routes/web.php');
    });
}

Also change your mapmethod:

还要改变你的map方法:

public function map()
{
    $this->mapAmpRoutes(); // <-- Add this

    $this->mapWebRoutes();

    $this->mapApiRoutes();
}

At this time all addresses like example.com/amp/...will refer to your web.phproutes.

此时所有的地址example.com/amp/...都将引用您的web.php路由。

Amp view files

放大器视图文件

Now you should customize view()helper to render a different view for amp. Create a helpers.phpin app/Httpdirectory with a view function:

现在您应该自定义view()助手来为放大器呈现不同的视图。使用视图函数创建一个helpers.phpinapp/Http目录:

function view($view = null, $data = [], $mergeData = [])
{
    $factory = app(Illuminate\Contracts\View\Factory::class);

    if (func_num_args() === 0) {
        return $factory;
    }

    //if amp, add '-amp' to view name
    if(request()->segment(1) == 'amp'){
        if(view()->exists($view . '-amp')){
            $view .= '-amp';
        }else{
            abort(404);
        }
    }
    return $factory->make($view, $data, $mergeData);
}

This function is not loaded until you add it to your bootstrap/autoload.phpfile

在您将其添加到bootstrap/autoload.php文件中之前,不会加载此函数

require __DIR__.'/../app/Http/helpers.php'; // <-- add this (should be before require vendor/autoload.php)
require __DIR__.'/../vendor/autoload.php';

Edit:If you don't find bootstrap/autoload.phpfile, search for vendor/autoload.phpbecause laravel has removed that. (Thanks MinderMondofor the comment)

编辑:如果找不到bootstrap/autoload.php文件,请搜索,vendor/autoload.php因为 laravel 已将其删除。(感谢MinderMondo的评论)

You can now add any views you like for amp with '-amp' affix. For example if you have index.blade.phpthe amp view name should be index-amp.blade.php.

您现在可以为带有“-amp”词缀的放大器添加任何您喜欢的视图。例如,如果您index.blade.php的放大器视图名称应该是index-amp.blade.php.

回答by Kristian Lilov

There are several ways of implementing AMP pages to your web application (no matter the framework).

有多种方法可以将 AMP 页面实施到您的 Web 应用程序(无论框架如何)。

One example is the way https://www.theguardian.comhave done this : by creating a new sumbomain "amp.theguardian.com" and every page on the main site "www.theguardian.com" has a canonical link to the same page, but the AMP version and vice versa.

一个例子是https://www.theguardian.com这样做的方式:通过创建一个新的 sumbomain“amp.theguardian.com”,并且主站点“www.theguardian.com”上的每个页面都有一个指向相同的页面,但 AMP 版本,反之亦然。

Example : Normal page : LINKAMP version of the page : LINK

示例:普通页面:页面的LINKAMP 版本:LINK

As you can see, they are the same, but not quite. Most of the elements that the normal page has are removed from the amp page (because AMP does not allow them or is a bad practice for AMP).

如您所见,它们相同,但不完全相同。普通页面具有的大多数元素都从 amp 页面中删除(因为 AMP 不允许它们或者对于 AMP 来说是一种不好的做法)。

So there you have it! A way to implement AMP to your Laravel project is to have separate views (resources/views/amp) and Controller that handles them.

所以你有它!在 Laravel 项目中实现 AMP 的一种方法是拥有单独的视图(资源/视图/放大器)和处理它们的控制器。

A good guide to what your AMP views can have as functionalities is HERE.

关于您的 AMP 视图可以具有哪些功能的一个很好的指南是这里

回答by Geding

I made one site with AMP and my solution was simple adding to routes new GET rule for: mobile/{articleSlug}/{pageNumber?}, and new function in Controller to support it, which load all necessary things for new AMP layout.

我用 AMP 制作了一个站点,我的解决方案很简单,将新的 GET 规则添加到路由中:mobile/{articleSlug}/{pageNumber?},以及 Controller 中的新函数来支持它,它为新的 AMP 布局加载所有必要的东西。

If I'm not wrong you can't have AMP page and normal page on this same route - Google will not know how to change between them.

如果我没记错的话,你不能在同一条路线上有 AMP 页面和普通页面 - 谷歌不会知道如何在它们之间进行更改。