In this article, we will learn how to easily set up **AMP (Accelerated Mobile Pages)** to the **Laravel**project.

AMP (Accelerated Mobile Pages) is a web component framework to create lightweight website and rank your website in the SEO world.

We will cover the following topics

  1. Laravel Installation
  2. Different Implementation Ways
  3. Existing Non-AMP Website Code Overview
  4. AMP Routes (mapAmpRoutes in RouteServiceProvider.php)
  5. AMP Controllers
  6. AMP View Pages
  7. Testing

Step 1 - Laravel Installation

If you already have the Laravel application skip to Step 2 else let’s install the Laravel application with composer using the following command

composer create-project --prefer-dist laravel/laravel blog

The above command creates a new Laravel project with a name **blog**.


Step 2 - Different Implementation Ways

NOTE: For the sake of the explanation I am using my website name. Make sure to change it to yours while development.

Basically we can achieve it in 2 ways, at least as far as I know.

1. Subdomain

Create a subdomain with **https://amp.stackcoder.in/post_url** and make all the content available on that site and even migrate all your code.

Disadvantages


i. You might end up using 2 codebases. If you change in one code base then you have to go and update in another one too.

ii. Sometime you may forget and if you’re using the same database for both then you will get errors in the production site.

iii. It may even impact your website SEO.

2. Same Domain With Prefixed Route (We will implement this)

We will create a prefixed route for this implementation which will look like the following

Normal Route - **https://stackcoder.in/posts/post_url**

AMP Route (Observe amp after the domain name**)** - **https://stackcoder.in/amp/posts/post_url**

Advantages


i. We will use most of the existing code base with minor changes to work with AMP.

ii. We will also use the existing web.php routes.

ii. No need to break our head on SEO optimization.

Disadvantages


i. If you modify anything on Non-AMP pages then even you have to modify in AMP pages

ii. You still have to create duplicate blade pages ie one for Non-AMP and one for AMP.

#php #laravel #amp

Setup AMP (Accelerated Mobile Pages) In PHP Laravel
81.50 GEEK